Jen T
asked on
VBA SQL statement - 2 "OR"s and 1 "And"
hi there,
I figured it would be faster asking a syntax question here than scouring google...
I just want to know the syntax for 2 or's and an And statement in SQL.
Currently I have:
Now I want to add " And WHERE Place = "Florida" or something like that.
Can I put "And WHERE" or is it "And Place = 'Florida'" ?
Thanks loads!
I figured it would be faster asking a syntax question here than scouring google...
I just want to know the syntax for 2 or's and an And statement in SQL.
Currently I have:
SQL = "SELECT DISTINCT qryEList.[Email Address] " & "FROM qryEList " & "WHERE GroupName = 'E' or GroupName = 'I'"
Now I want to add " And WHERE Place = "Florida" or something like that.
Can I put "And WHERE" or is it "And Place = 'Florida'" ?
Thanks loads!
ASKER
Oh. I figured it out. I had to enclose the "OR" statements in parenthesis. That did it... Sorry.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just as with arithmetic, there is a logical order of precedence applied to compound conditions.
For the common ones it is Parentheses, NOT, AND, OR
So the expression evaluator parses the compound expression and if it finds any Parentheses does them first, then the NOTs, then the AND's and finally the OR's.
If NOT a OR b OR c AND d is evaluated as:
If (NOT a) OR b OR (c AND d)
If that isn't what you had in mind, then, you need to add parentheses to control the order. Whenever I create a compound condition that includes different operands, I always use parentheses even if the expression parser will evaluate it correctly because the parentheses clarify the expression for everyone and if it needs to be modified will help the person to change it correctly.
In your case, you wanted,
If (a OR b) AND c rather than If a OR (b AND c)
An answer to this question deserves some explanation rather than a just try this so you will understand the logic and be able to apply it in other situations.
For the common ones it is Parentheses, NOT, AND, OR
So the expression evaluator parses the compound expression and if it finds any Parentheses does them first, then the NOTs, then the AND's and finally the OR's.
If NOT a OR b OR c AND d is evaluated as:
If (NOT a) OR b OR (c AND d)
If that isn't what you had in mind, then, you need to add parentheses to control the order. Whenever I create a compound condition that includes different operands, I always use parentheses even if the expression parser will evaluate it correctly because the parentheses clarify the expression for everyone and if it needs to be modified will help the person to change it correctly.
In your case, you wanted,
If (a OR b) AND c rather than If a OR (b AND c)
An answer to this question deserves some explanation rather than a just try this so you will understand the logic and be able to apply it in other situations.
ASKER