Trouble with And/Or Operator

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I seem to be having trouble with a simple AND/OR Operator it give me a
runtime error – Type Missmatch.
IF Me.cboFastener.Column(1) = "Washer" OR "Nut" THEN
It works fine if I use 2 IF statements one for "Washer" and one for "Nut"
And it works OK using a CASE SELECT.
Am I missing a reference library or something for the AND/OR Operator?
 
I seem to be having trouble with a simple AND/OR Operator it give me a
runtime error – Type Missmatch.
IF Me.cboFastener.Column(1) = "Washer" OR "Nut" THEN
It works fine if I use 2 IF statements one for "Washer" and one for "Nut"
And it works OK using a CASE SELECT.
Am I missing a reference library or something for the AND/OR Operator?

Not that I'm aware of, but you can do this:

If me.cboFastener.Column(1) = "Washer" Or me.cboFastener.Column(1) = "Nut" then
<do something>
End IF

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
IF Me.cboFastener.Column(1) = "Washer" OR "Nut" THEN

Scott has given you the common-sense answer. but the reason for the "type
mismatch" error is this:

The phase above is syntactically equivalent to

If (Me.cboFastener.Column(1) = ("Washer" OR "Nut")) Then

The first expression to be evaluated is ("Washer" OR "Nut"); now OR only
takes two numeric operands, and neither of these can be forced into a
numeric value. so that is where the error comes from. This would work
fine of course:
("3" OR "6")
and would return the value 7 as expected.

What you want to do is to combine to comparisons, so you have to carry
out two comparisons:

(Me.cboFastener.Column(1) = "Washer") OR _
(Me.cboFastener.Column(1) = "Nut")

The brackets are strictly unnecessary, but make everything very obvious.

Hope that helps


Tim F
 
Tim said:
Scott has given you the common-sense answer. but the reason for the "type
mismatch" error is this:

The phase above is syntactically equivalent to

If (Me.cboFastener.Column(1) = ("Washer" OR "Nut")) Then

The first expression to be evaluated is ("Washer" OR "Nut"); now OR only
takes two numeric operands, and neither of these can be forced into a
numeric value. so that is where the error comes from. This would work
fine of course:
("3" OR "6")
and would return the value 7 as expected.

What you want to do is to combine to comparisons, so you have to carry
out two comparisons:

(Me.cboFastener.Column(1) = "Washer") OR _
(Me.cboFastener.Column(1) = "Nut")

The brackets are strictly unnecessary, but make everything very obvious.


Tim, your logic/conclusion/explanation is accurate, but the
premiss is flawed ;-)

= has higher precedence than OR

The phase is syntactically equivalent to
If (Me.cboFastener.Column(1) = "Washer") OR "Nut" Then
 
Case select has two differences:

1) It corrected your logic, so that you didn't have a condition like
If "Nut" Then

2) It only evaluates until it finds a true condition, so you can put
similar invalid logic without noticing an error until it is too late:
select case 1
case: 1, "nut"
will have no error, but
select case 1
case: 2, "nut"
will have the same type mismatch error you started with

(david)
 
If (Me.cboFastener.Column(1) = ("Washer" OR "Nut")) Then
does not work
If (Me.cboFastener.Column(1) = "Washer") OR (Me.cboFastener.Column(1) =
("Nut") Then
Does Work
 
Tim, your logic/conclusion/explanation is accurate, but the
premiss is flawed ;-)

= has higher precedence than OR

The phase is syntactically equivalent to
If (Me.cboFastener.Column(1) = "Washer") OR "Nut" Then

Whoops. Thanks Marsh, you are of course correct.

? "3" = 2 or 8
8

which kind of proves it.

All the best


Tim F
 
Back
Top