Combining AND and OR statements in Filters

J

Jamie

Hi, I'm trying to write the code for a Filter to a query.

strFilter = strFilter & " AND [nStatus] <> 3 OR " & strFilter & " AND
[nStatus] <> 4"

I need help with the syntax and ordering to make sure it satisfies BOTH
condition 1 and condition 2 or BOTH condition 1 and condition 3.

Thanks
Jamie
 
M

Michel Walsh

You can use parenthesis:

(condition1 AND condition2) OR (condition1 AND condition3)


(which is logically equivalent to : condition1 AND (condition2 OR
condition3) )


strFilter = "(" & strFilter & " AND [nStatus] <> 3 ) OR (" & strFilter & "
AND [nStatus] <> 4)"

or

strFilter = strFilter & " AND ( [nStatus] <> 3 OR [nStatus] <> 4)"



but that won't do, because it seems there is a problem of intention..
Indeed, if nStatus = 3, is it <> 4. So, status<>4 will be true and you
will keep the record, even if you probably don't want record which have
either 3 either 4:

strFilter = strFilter & " AND NOT ( [nStatus] = 3 OR [nStatus] = 4)"


which is also the same as


strFilter = strFilter & " AND NOT [nStatus] IN(3, 4)"




Vanderghast, Access MVP
 
R

Rick Brandt

Jamie said:
Hi, I'm trying to write the code for a Filter to a query.

strFilter = strFilter & " AND [nStatus] <> 3 OR " & strFilter & " AND
[nStatus] <> 4"

I need help with the syntax and ordering to make sure it satisfies
BOTH condition 1 and condition 2 or BOTH condition 1 and condition 3.

Thanks
Jamie

You just need to add parenthesis.

(This And That) Or (These And Those)

In your specific case it looks like you could dispense with the "Or"
though...

strFilter = strFilter & " AND [nStatus] Not In(3, 4)"
 
J

Jamie

Thankyou both.

It works and tidies up my other filters.


Rick Brandt said:
Jamie said:
Hi, I'm trying to write the code for a Filter to a query.

strFilter = strFilter & " AND [nStatus] <> 3 OR " & strFilter & " AND
[nStatus] <> 4"

I need help with the syntax and ordering to make sure it satisfies
BOTH condition 1 and condition 2 or BOTH condition 1 and condition 3.

Thanks
Jamie

You just need to add parenthesis.

(This And That) Or (These And Those)

In your specific case it looks like you could dispense with the "Or"
though...

strFilter = strFilter & " AND [nStatus] Not In(3, 4)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top