sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Missionary's Avatar

Conditional IIF in query criteria


Question posted by: Missionary (Newbie) on August 27th, 2008 04:06 PM
I have a form that displays data from a query, and I want to be able to control what data is displayed with a combo box on the form.
In the row source of the combo box, I have a list of values, but I also have some qualifiers (All, Blanks, NonBlanks, etc.)

I wrote a criterion that will show "All" or one of the values and it works well (see below.)

Like IIf([Forms]![frmNewE]![Arealu]="All","*",[Forms]![frmNewE]![Arealu])

But When I tried to include other types of qualifiers , I couldn't get it to work:

IIf([Forms]![frmNewE]![Arealu]="All",Like "*",IIf([Forms]![frmNewE]![Arealu]="Blanks",Is Null Or "",IIf([Forms]![frmNewE]![Arealu]="NonBlanks",Is Not Null And Not "" , Like [Forms]![frmNewE]![Arealu])))

It seems like you can't have a "Like" statement within an IIF Statement, but I'm not sure.
Can anyone help me with this syntax? Much Thanks.
4 Answers Posted
FishVal's Avatar
FishVal August 27th, 2008 04:37 PM
Expert - 1,991 Posts
#2: Re: Conditional IIF in query criteria

Hello.

Database engine is not supposed to run functions inside SQL expression to get missing part(s) of SQL expression itself. ;)

I see that your problem is that you couldn't figure out what IIf() should return as an argument to Like operator in order to fetch records with Null value in the field being filtered by.

I suggest you to change query syntax:
instead of
Expand|Select|Wrap|Line Numbers
  1. SELECT [Field] FROM [Table] WHERE [Field] Like IIf([Forms]![frmNewE]![Arealu]="Blanks",Is Null Or "", ...);

try
Expand|Select|Wrap|Line Numbers
  1. SELECT [Field] FROM [Table] WHERE [Forms]![frmNewE]![Arealu]="Blanks" AND Nz([Field], "")="";

And so on with the rest two user choices.

Regards,
Fish
Missionary's Avatar
Missionary August 27th, 2008 06:17 PM
Newbie - 30 Posts
#3: Re: Conditional IIF in query criteria

Thanks alot!
That worked like a prayer.

Now when it follows the "All" criteria:
Expand|Select|Wrap|Line Numbers
  1. tblEquip.[Field] Like "*" 

It doesn't include null values. Is there an easy way to include all records?

I came up with this:
Expand|Select|Wrap|Line Numbers
  1. tblEquip.[Field] Like "*" OR Nz(tblEquip.[Field], "")=""


But it seems messy. Is there a better way?
FishVal's Avatar
FishVal August 28th, 2008 07:44 AM
Expert - 1,991 Posts
#4: Re: Conditional IIF in query criteria

Oh, yes.

Criteria like
Expand|Select|Wrap|Line Numbers
  1. ... WHERE [Forms]![frmNewE]![Arealu]="All" ...

will enforce fetching all records in a case user choice is "All"

Regards,
Fish

P.S. You may read a similar thread - How to show all values in a query iif criteria?
Missionary's Avatar
Missionary August 28th, 2008 05:57 PM
Newbie - 30 Posts
#5: Re: Conditional IIF in query criteria

Perfect. Thank you so much. This is working out better than I expected.

So now I have the following, which works perfectly:

Expand|Select|Wrap|Line Numbers
  1. WHERE((([Forms]![frmNewE]![Arealu]="Blanks" AND (tblEquip.Area IS NULL OR tblEquip.Area="")) 
  2. OR ([Forms]![frmNewE]![Arealu]="All") 
  3. OR ([Forms]![frmNewE]![Arealu]="NonBlanks" AND ((tblEquip.Area) Is Not Null And Not (tblEquip.Area)="")) 
  4. OR (tblEquip.Area like [Forms]![frmNewE]![Arealu])) )


The problem is that in the form that I am using, there are 18 fields that I am sorting by. So I put in similar code in the query for each field. And this works fine until I get too many of them, then I get an error message, that says the expression is too complex to be evaluated and that I should simplify the code by assigning parts of the expression to variables.

Any thoughts on how I can simplify it?


Quote:
Originally Posted by FishVal
Oh, yes.

Criteria like
Expand|Select|Wrap|Line Numbers
  1. ... WHERE [Forms]![frmNewE]![Arealu]="All" ...

will enforce fetching all records in a case user choice is "All"

Regards,
Fish

P.S. You may read a similar thread - How to show all values in a query iif criteria?
Reply
Not the answer you were looking for? Post your question . . .
197,038 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,038 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Microsoft Access / VBA Contributors