If statement being bypassed? (1 Viewer)

George-Bowyer

Registered User.
Local time
Today, 23:54
Joined
Dec 21, 2012
Messages
177
I have a form that allows the user to appoint members to various positions in a club.

If the position is of a certain type, then I want the code to do certain things.

For some reason, the code is running regardless of the position

This is the relevant nugget from the beginning of a very long If clause.

Code:
        If intPosition = 5 Or 55 Then 
        
            MsgBox intPosition
            Exit Sub

It seems to me that the msgbox should only come up if intPosition is 5 or 55.

but it's popping up regardless of the number.

How?

I am confused. This seems really basic level stuff that doesn't seem to be working
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:54
Joined
May 7, 2009
Messages
19,169
If intPosition = 5 Or intPosition = 55 Then
 

George-Bowyer

Registered User.
Local time
Today, 23:54
Joined
Dec 21, 2012
Messages
177
Doh! Ok, thanks, that makes sense.

Should probably be using a select case anyway.

So what I effectively wrote was:

If 55 then...

Question is why does that trigger the if clause? What is the code deciding equals 55 to meet the criteria?
 

Minty

AWF VIP
Local time
Today, 23:54
Joined
Jul 26, 2013
Messages
10,355
Stealing something from another thread. In the immediate window type

? IIf(55,"yes", "no")

and hit return.
You are effectively saying is " Is 55 ?" which of course is yes, it's 55?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:54
Joined
May 7, 2009
Messages
19,169
it's because you are Checking a Constant:

If 55 then..

is checking if number 55 is 55, which is True.

so:

If intPosition = 5 Or 55 then..

access will evaluate it as:

If (intPosition = 5) Or (55 = 55) then ...

which is Always true (55 = 55), so this IF will be executed.
 

George-Bowyer

Registered User.
Local time
Today, 23:54
Joined
Dec 21, 2012
Messages
177
Ah. I see.

Sort of.

I see not to do it again, anyway.

What is now going to give me sleepless nights is wondering how many other times I have made that mistake in other dbs over the past couple of decades or so... :eek::eek::eek:
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 07:54
Joined
May 7, 2009
Messages
19,169
SELECT CASE intPosition
CASE 5, 55
...
END SELECT
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 18:54
Joined
Feb 28, 2001
Messages
27,001
This is an implementation-dependent thing, but... technically that IF statement immediately condones a Boolean argument.

IF intPosition = 5 Or 55 will be parsed as though it were IF ( intPosition = 5 ) Or 55 because of operator precedence rules. Obviously, the case of intPosition = 5 will trigger the sequence. BUT... remember that the IF forced this to be evaluated as Boolean. So what is the truth value of 55? I believe that the rule for VBA is that truth is a zero/non-zero test, with zero being FALSE. Well, 55 is non-zero so it counts as TRUE. Then, the OR of a TRUE value is always TRUE. So that is the detailed explanation of why it did what it did.
 

Isaac

Lifelong Learner
Local time
Today, 16:54
Joined
Mar 14, 2017
Messages
8,738
I tested TDM's explanation for fun and it worked as stated!

?iif(0,"yes","no")
no

This is an implementation-dependent thing, but... technically that IF statement immediately condones a Boolean argument.

IF intPosition = 5 Or 55 will be parsed as though it were IF ( intPosition = 5 ) Or 55 because of operator precedence rules. Obviously, the case of intPosition = 5 will trigger the sequence. BUT... remember that the IF forced this to be evaluated as Boolean. So what is the truth value of 55? I believe that the rule for VBA is that truth is a zero/non-zero test, with zero being FALSE. Well, 55 is non-zero so it counts as TRUE. Then, the OR of a TRUE value is always TRUE. So that is the detailed explanation of why it did what it did.
 

Users who are viewing this thread

Top Bottom