473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Conditional IIF in query criteria

Missionary
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.
Aug 27 '08 #1
5 10289
FishVal
2,653 Expert 2GB
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 "", ...);
  2.  
try
Expand|Select|Wrap|Line Numbers
  1. SELECT [Field] FROM [Table] WHERE [Forms]![frmNewE]![Arealu]="Blanks" AND Nz([Field], "")="";
  2.  
And so on with the rest two user choices.

Regards,
Fish
Aug 27 '08 #2
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?
Aug 27 '08 #3
FishVal
2,653 Expert 2GB
Oh, yes.

Criteria like
Expand|Select|Wrap|Line Numbers
  1. ... WHERE [Forms]![frmNewE]![Arealu]="All" ...
  2.  
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?
Aug 28 '08 #4
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?


Oh, yes.

Criteria like
Expand|Select|Wrap|Line Numbers
  1. ... WHERE [Forms]![frmNewE]![Arealu]="All" ...
  2.  
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?
Aug 28 '08 #5
FishVal: Thank you so much for that! I was doing the exact same problem trying to work a query that I wanted to search for the First or Last Name, but I wanted to let the user pick either field from the Search Modal Dialog. You just saved me from going crazy.
Nov 12 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: chandy | last post by:
Hi, I am trying to change an SP from dynamic SQL to proper SQL but I can't figure a way to conditionally add extra parts to the statement. How can I do the equivalent of the following? ...
4
by: Steve | last post by:
How do I write the conditional If statement for a query criteria where if Forms!MyForm!MyCbx = "AllCategories" then Like "*" else Forms!MyForm!MyCbx ? I can't get the Like "*" to work. Thanks!...
3
by: B Love | last post by:
I would like to display a drop-down list on a form conditional on the value of another field on the same form. I am not sure if this is good form or not. I am using Access97, but have access to...
6
by: Andy | last post by:
Hello, I am having many problems with setting up a parameter query that searches by the criteria entered or returns all records if nothing is entered. I have designed an unbound form with 3...
5
by: SuffrinMick | last post by:
Hello - I'm a newbie to coding! I'm working on an access 2000 database which has three tables: tblContacts - A list of customer contacts. tblOrgTypes - A list of organisational types....
3
by: MLH | last post by:
Am repeating question with different subject heading, perhaps stating more clearly my problem... I have an A97 query (qryVehiclesNowners2) that has a table field in it named . Depending on the...
2
by: Piper707 | last post by:
Hi, I need to know if there is any way of achieving conditional processing for XSD elements. --------------------------------------------------------------- <criteria></criteria> = a complex...
3
by: msrviking | last post by:
Hello everybody, After several attempts of writing the query, I had to post my requirement in the forum. Here is what I have, what I need and what I did. Table A Col1 Col2 1 Nm1
2
by: enywu | last post by:
Hi all, I have created a report in my access database through a query. The report has no problem, however now there is new request that they want this report to use the conditional formatting....
1
by: veaux | last post by:
Question deals with linking tables in queries. I'm not a code writer so use the GUI for all my queries. Table 1 - Master Table 2 - Sub1 Table 3 - Sub 2 All 3 tables have the same key field....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.