Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If not variable_1, variable_2 or variable_3 then msgbox

Status
Not open for further replies.

dontay

Programmer
Jan 22, 2003
49
US
OK I'm not a profesional programer but I do like making Access aplications with VBA.
I'm stuck on a simple argument:

If not variable_1, variable_2 or variable_3 then
msgbox

Any clues as to the syntax for doing this in VBA???

I could maybe figure out a funcky way of doing it but it wouldn't be efficient.

Thanks in advance
Don
 
Did you check the VBA help file for IF..THEN..ELSE? Tell us what you going to accomplish. It is difficult to say how to use ithout knowing what you are going to do
Any how we have 7 forums dedicated to MS Access are below
Microsoft: Access Forms Forum
Microsoft: Access Modules (VBA Coding) Forum
Microsoft: Access Queries and JET SQL Forum
Microsoft: Access Reports Forum
Microsoft: Access Tables and Relationships Forum
Microsoft: Access Other topics Forum
Microsoft: Access Project (ADP) Forum


________________________________________
Zameer Abdulla
Visit Me
Minds are like parachutes. They only function when they are open. -Sir James Dewar (1877-1925)
 

If x<>Var_1 or x<>var_2 or x<>var_3 then
msgbox(cstr(x)+" is not equal to any of the values in the list")
else
msgbox(cstr(x)+" matches one of the values in the list")
end if
 
ETID, your logic is wrong, you should be using AND instead of OR.

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Ahhhhhhh. So glad that really knowledgeable people have "doh" days too.

Gerry
 
Thank you all,
If I use "AND" then all 3 variables have to match x. I'm trying to have the msgbox pop up when none of the individual variables match x.
I got it to work with:

If x Like var_1 Or x Like var_2 Or x Like var_3 Then
MsgBox " x is equal to one of the variables in the list"
End If

But what I need is for it to be reversed. If x NOT like var_1 or var_2 or var_3 then
Msgbox "x is not in the list"

Thanks for any thoughts
Don
 
Duh,
The else statment gives me the reverse.

If x Like var_1 Or x Like var_2 Or x Like var_3 Then
MsgBox " x is equal to one of the values in the list"
Else
MsgBox "x is not equal to any of the values in the list "
End If

Thanks for helping me

I really do appreciate you all.

Don
 
tested...
Code:
   Dim myvar As Integer
    myvar = Me.txtTestBox.Value
    If Not myvar <> 0 Or Not myvar <> 2 Or Not myvar <> 3 Then
        MsgBox "Not matching"
    Else
        MsgBox "Matching"
    End If

________________________________________
Zameer Abdulla
Visit Me
If you have never been hated by your child, you have never been a parent.[/color]
 
Hi ZmrAbdulla,

and, can you explain what you tested exactly? As far as I can see you code will ALWAYS display a message of "Not matching".

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
If the value of the textbox(txtTextBox)=0 or 2 or 3 then it will say "Not matching" otherwise it will say "matching". I used double negative style
Not myvar <> 0
I have tested it with a textbox and command button again it is working.
even this
Code:
Private Sub cmdTest_Click()
    Dim myvar As [b][COLOR=blue]Variant[/color][/b]
    myvar = Me.txtTestBox.Value
    If Not myvar <> 0 Or Not myvar <> 2 Or Not myvar <> [b][COLOR=blue]"Y" [/color][/b]Then
        MsgBox "Not matching"
    Else
        MsgBox "Matching"
    End If
End Sub

________________________________________
Zameer Abdulla
Visit Me
If you have never been hated by your child, you have never been a parent.
 
It was the double negative I didn't spot ... mainly because it is not needed, in other words your code is the same as
Code:
Private Sub cmdTest_Click()
    Dim myvar As Variant
    myvar = Me.txtTestBox.Value
    If myvar = 0 Or myvar = 2 Or myvar = "Y" Then
        MsgBox "Not matching"
    Else
        MsgBox "Matching"
    End If
End Sub
that, together with your misleading MsgBox message of "Not matching" when myvar matches a value, really confused me.

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Also, what you've done doesn't match what dontay requested, which is what I thought you were trying to do.

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Yes.. it is same as "=" operator. But I got an error in my first test. So I tried the double negative.
Also my message box is reversed. (May be my understanding of english is....) here I confused
dontay said:
But what I need is for it to be reversed. If x NOT like var_1 or var_2 or var_3 then
Msgbox "x is not in the list"


________________________________________
Zameer Abdulla
Visit Me
If you have never been hated by your child, you have never been a parent.
 
Hi ZmrAbdulla,

dontay was a little confused in his post, not understanding that mathematically if x <> var_1 AND x <> var_2 AND x <> var_3 was exactly what he wanted ( i.e. "x is not var_1 or var_2 or var_3" in English is the same as "x is not var_1, and it is also not var_2, and it is also not var_3" ).

Cheers, Glenn.

Did you hear about the literalist show-jumper? He broke his nose jumping against the clock.
 
Now it is time for dontay to decide Match or not. We will wait....

________________________________________
Zameer Abdulla
Visit Me
If you have never been hated by your child, you have never been a parent.
 
You may try either this:
If x Not Like var_1 And x Not Like var_2 and x Not Like var_3 Then
MsgBox "x is not equal to any of the values in the list"
End If
Or this:
If Not (x Like var_1 Or x Like var_2 Or x Like var_3) Then
MsgBox "x is not equal to any of the values in the list"
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Private Sub Command9_Click()

I swear this site is great!
This is the one that works for me.
It's those parentheses that I couldn't figure out.
Thanks every one.
Expecially PH.

Don
*****************************************
Dim x As Variant
Dim var_1 As Variant

Dim var_2 As Variant
Dim var_3 As Variant
x = Me.StoreCode

var_1 = "VA5063"
var_2 = "VA4155"
var_3 = "VA5060"

If Not (x Like var_1 Or x Like var_2 Or x Like var_3) Then
MsgBox "x is not equal to any of the values in the list"
End If


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top