Full Version: Using Iif And Like In A Criteria
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
Spallen
I am trying to use a check box from a form to filter a query. If the box is checked I want all records like "sm*" otherwise I want all records not like "sm*"

As a test I have been trying

IIf(1=1,"like & '*'")

in the Criteria box, I can't seem to get any records to display. I can't figure out how to pass the like "*" from the IIF statement.

I also tried
IIf(1=1,"like *")
IIf(1=1,"like '*'")

Thanks.
theDBguy
Hi,

welcome2UA.gif

Try something like:

Like "sm*" And Forms!FormName.CheckboxName = True
Not Like "sm*" And Forms!FormName.CheckboxName = False

Note: Enter each line into separate criteria rows under the column/field you want to filter.

(untested)
Hope that helps...
Spallen
The core concept worked. I had to change things to fit what I have but it worked.

Here is my final code.

Like "SM*" And [Forms]![Servicetracker]![Service-C]="-1" Or Not Like "SM*" And [Forms]![Servicetracker]![Other-C]="-1"

Service-C is one check box and Other-C is another. when none are checked no results, if 1 checked I get either like or not like, and if both checked I get everything.
theDBguy
Hi,

Glad to hear you got it to work. Just a couple of comments:

1. Be careful when using multiple logical operators in your criteria. You have to be aware of their precedence order so you don't get unexpected results.
2. If [Service-C] is a Checkbox (Yes/No) type, I don't think you're supposed to enclose "-1" in quotes.

Good luck with your project.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.