Wildcarding in an IIf Statement

T

TESA0_4

I have a table where work done on aircraft is broken down into zones and
qualifiers applicable to those zones. For example 'fuselage' has qualifiers
such as 'front', rear', 'flight deck'etc.
Users need to query the data based on zone and qualifiers.
I have created a form where users can select zones and then get a filtered
list of qualifiers applicable to the zone. They have the option of selecting
up to three qualifiers for a zone. But I also want them to have the option
for selecting all records for a zone irrespective of qualifier. With this in
mind my form has a flag for selecting 'All Qualifiers'.

In the Design View for the query I have entered the following criterion for
the Qualifier field:
IIf([Forms]![frmAnalysis]![FlagAll]=-1, Like
"*",[Forms]![frmAnalysis]![QualifierA] Or [Forms]![frmAnalysis]![QualifierB]
Or [Forms]![frmAnalysis]![QualifierC])

If I enter a value in the form for QualifierA, B and/or C, the results are
fine. If no Qualifier values are entered and the Flag is set to Yes, the
query returns no records. Why is 'Like "*"' not returning a wildcard search
for any Qulaifier value?

Any sugesstions gratefully received.
 
J

John W. Vinson

IIf([Forms]![frmAnalysis]![FlagAll]=-1, Like
"*",[Forms]![frmAnalysis]![QualifierA] Or [Forms]![frmAnalysis]![QualifierB]
Or [Forms]![frmAnalysis]![QualifierC])

If I enter a value in the form for QualifierA, B and/or C, the results are
fine. If no Qualifier values are entered and the Flag is set to Yes, the
query returns no records. Why is 'Like "*"' not returning a wildcard search
for any Qulaifier value?

Because IIF can only return an actual value to be searched - NOT an operator
such as LIKE or OR.

Try turning the logic around: use a criterion of

IN ([Forms]![frmAnalysis]![QualifierA], [Forms]![frmAnalysis]![QualifierB],
[Forms]![frmAnalysis]![QualifierC]) OR [Forms]![frmAnalysis]![FlagAll]=-1

This will return all records if FlagAll is True (since the criterion evaluates
to TRUE regardless of the table contents); if FlagAll isn't TRUE it uses the
IN syntax to match any of the three form controls.
 
T

TESA0_4

Thanks for your response John.

I have inserted your suggestion and it does exactly what I want. However, I
don't understand why it works!

My presumption was that an expression used to determine a Query condition
needed to resolve to a value that in someway related to the values
potentially stored in the field. Your expression shows me that an expression
that resolves to 'True' allows all values stored in the field. I don't really
understand why this should be so but I can understand the usefulness of
arrangement.

Thanks for solving my immediate problem and expanding my understanding of
Access.

Regards,

Terry
 
J

John W. Vinson

My presumption was that an expression used to determine a Query condition
needed to resolve to a value that in someway related to the values
potentially stored in the field. Your expression shows me that an expression
that resolves to 'True' allows all values stored in the field. I don't really
understand why this should be so but I can understand the usefulness of
arrangement.

That is a little appreciated feature of SQL queries. A SQL query may have a
WHERE clause (and/or a HAVING clause if it's a totals query).

The WHERE clause is a logical expression which must evaluate to either TRUE or
FALSE.

If it's TRUE, the current record is included in the query recordset. If it's
FALSE, it isn't.

There is *nothing* in the specifications that requires that the WHERE clause
must reference table fields!

There are in fact circumstances where it makes sense to use

SELECT <whatever> FROM table WHERE TRUE;

to retrieve all records, or

SELECT <whatever> FROM table WHERE FALSE;

to intentionally retrieve an empty recordset (say because you're going to use
it to add records via a form or code).
 
T

TESA0_4

Hi John,
Thank you for the explanation. I understand the concept but at this stage my
skills with writing SQL are very limited. I'm still at the stage of mastering
the use the criteria grid in the Query Design View and from time to time
referring to the SQL view to try and comprehend the code that is generated.
For example, since receiving your reply yesterday I have mastered the concept
incorporating 'flag fields' in the grid (the way in which Access 'interprets'
the code you suggested yesterday).

Regards,
Terry
 

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