I have an Access 2007 query. In the criteria, I need it to select all records if a filter in a form is set to “View all organisations”. However, I can’t get it to work. Criteria below:
=IIf([Forms]![Reports]![Referrals - Business Filter]="View all organisations","Like '*'")
The criterion works in general – if I change “Like ‘*'” to something specific, e.g. “Barclays”, then it selects the correct contacts listed under that business, but that’s not what I want to achieve.
What am I doing wrong?
Thanks.
You’ve hit on something interesting! The “Like *” you’re trying to get the query to evaluate is not a literal string, but a logical expression (which evaluates to True or False).
When you’re switching between different comparison expressions in SQL using Iif (strictly, Iif is Access VBA, not SQL, but Access allows you to use VBA to an extent in queries), you can only switch between literal values. e.g. 1, 453.5, “This”, “A customer name”.
Switching to a “LIKE *” expression amounts to writing SQL on the fly. It’s like me saying “I ‘ve had (Iif(It’s Friday, (5-4), (6/2)) cups of coffee today”. Your brain can handle this switch in context from literal English to something that has to be evaluated (and amounts, today, to the English literal word “one”); so can Access – to a certain extent. But not to the extent of evaluating this:
and interpreting it to mean
THe way round it is to break out the check whether “View all” is selected into a separate test: