Combine Statements (1 Viewer)

kitty77

Registered User.
Local time
Yesterday, 19:48
Joined
May 27, 2019
Messages
712
How would I combine these statements? Default for image1 is not visible.

If Me.Mdid = "ABC123" Then Me.image1.Visible = True Else Me.image1.Visible = False
If Me.Mdid = "DEF123" Then Me.image1.Visible = True Else Me.image1.Visible = False
If Me.Mdid = "GHI123" Then Me.image1.Visible = True Else Me.image1.Visible = False
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 19:48
Joined
Feb 19, 2002
Messages
43,257
There are better ways than hardcoding this If statement but you would have to expand your mind and tell us what problem you are trying to solve.
Code:
If Me.Mdid = "ABC123" Or Me.Mdid = "DEF123" or Me.Mdid = "GHI123" then
    Me.image1.Visible = True
Else
    Me.image1.Visible = False
End If
 

kitty77

Registered User.
Local time
Yesterday, 19:48
Joined
May 27, 2019
Messages
712
This is perfect. Just what I was looking for. Thanks!
 

KitaYama

Well-known member
Local time
Today, 08:48
Joined
Jan 6, 2022
Messages
1,540
I always use it this way :

Code:
image1.Visible = InStr(",ABC123,DEF123,GHI123,",  "," & Mdid & ",") > 0
 
Last edited:

kitty77

Registered User.
Local time
Yesterday, 19:48
Joined
May 27, 2019
Messages
712
Can you explain what each part is doing?
 

KitaYama

Well-known member
Local time
Today, 08:48
Joined
Jan 6, 2022
Messages
1,540
English is not my native language and it's really hard to explain what's going on there. I only have learnt it over the years I'm playing with Access.
I hope someone else pop in and explain it.

For now:
Since the code is running in a form's module, I never use Me before controls name. I may be wrong but it makes the code simpler to read.
It means that I use Mdid instead of Me.Mdid.

image1.Visible =
Left part of the equal sign is used to set the property. image1.Visible = means I want to set a value for the visibility of image1.
In the right side I have a function to return a False Or True.

InStr("ABC123,DEF123,GHI123,", Mdid & ",") > 0
InStr function checks the position of the first occurrence of one string within another.
I'm asking Instr to see if the position of Mdid & "," in "ABC123,DEF123,GHI123," is greater than 0 or not.

If instr returns 0, it means that Mdid & "," can not be found in "ABC123,DEF123,GHI123,"
This causes the right side of the equal sign to return a False value.
And image1.Visible = Flase

Otherwise
If instr returns a number greater than 0, it means that Mdid & "," can be found in "ABC123,DEF123,GHI123,"
This causes the right side of the equal sign to return a True value.
And image1.Visible = True


Note : The , before and after Mdid is added just to be certain for an exact check and prevent error if Mdid is empty or null
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 19:48
Joined
Feb 19, 2002
Messages
43,257
Using Me. in the form/report module makes it obvious that you are referencing a control rather than a local variable or tempvar so it is helpful to the reader and any subsequent developer. It also saves the interpreter some work since it tells the interpreter where the variable is defined.

As I said, you may not want to use this solution anyway but we'd need to know the scope of what you are hardcoding before offering a more maintainable solution.
 

Users who are viewing this thread

Top Bottom