Combining AND and OR statements in Filters

  • Thread starter Thread starter Jamie
  • Start date Start date
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
 
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
 
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)"
 
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)"
 
Back
Top