Multiple Criteria using IIF

seabass

New member
Local time
Today, 00:02
Joined
Apr 7, 2006
Messages
6
:eek: I am looking for a method that works in query criteria (NOT HARDCODING SQL) as i am quite new to this.:eek:

i have two fields for query to lookup, firstname and lastname.

THE FIRSTNAME FIELD:

IIf(IsNull([Forms]![frmSearch]![FirstName]),"",[Forms]![frmSearch]![FirstName])

THE LASTNAME FIELD:

IIf(IsNull([Forms]![frmSearch]![LastName]),"",[Forms]![frmSearch]![LastName])



Intended operation;
1) The value is seen on my search form (frmsearch) and returned to FIRSTNAME and LASTNAME query fields as NULL or VALUE.

2) If on the search form FIRSTNAME has value and LASTNAME is NULL then query should lookup a record with valid FIRSTNAME and nothing in LASTNAME.

3) If on the search form LASTNAME has value and FIRSTNAME is NULL then query should lookup a record with valid LASTNAME and nothing in FIRSTNAME.

4) If on the search form FIRSTNAME and LASTNAME have value (NOT NULL) then lookup record with valid FIRSTNAME and LASTNAME.

5) If no information is entered on the search form then "enter some information" as message box.

6) I would want wildcard using which also doesn't seem to work in IIF statement. e.g. "*" & [Forms]![frmSearch]![FirstName] & "*"

THERE FORE ---> IIf(IsNull("*" & [Forms]![frmSearch]![FirstName] & "*"),"","*" & [Forms]![frmSearch]![FirstName] & "*")



ONLY NUMBERS ABOVE (2) (3) AND (6) DO NOT WORK, IF ANYONE COULD GIVE SOME LIGHT ON THIS I WOULD BE VERY GRATEFUL, AS I AM IN THE BLUE ON THIS.:rolleyes:

TAKE IT EASY:D
 
In an IIF expression, you can use the word True to return all the records when the control on the form is null.


Put the two criteria in two new columns in the query grid like these:
------------------------------------------
Field: IIf(IsNull([Forms]![frmSearch]![FirstName]), True, [FirstName] Like "*" & [Forms]![frmSearch]![FirstName] & "*")

Show: uncheck

Criteria: True
------------------------------------------

------------------------------------------
Field: IIf(IsNull([Forms]![frmSearch]![LastName]), True, [LastName] Like "*" & [Forms]![frmSearch]![LastName] & "*")

Show: uncheck

Criteria: True
------------------------------------------


Take a look at this thread:
Query By Form - using Text Boxes and Combo Boxes in criteria
http://www.access-programmers.co.uk/forums/showthread.php?t=103312
.
 
A great big THANKYOU

Hello Jon,

It worked, but please read on... ;)

I have to admit that I had been on this problem for a long time. Too long! :rolleyes:

I looked at the code you provided and I realise the main part I got wrong was not linking the criteria in question to the expression (the TRUE bit).

:) I'm on a painful learning curve and I promise it's painful :cool: finding how access works is difficult and just like everyone else, we all know an SQL book won't explain the problem in hand or just like any other reference book. :)

So many people on Access forums and reference sites have had so many headaches trying to solve this... I am glad that people can find this solution right here.

You have given me the handle to the door I needed to open!!! :D

I thankyou for your unbelievibly quick reply,
Best Wishes,

Mr G. Healy
 
Multiple Criteria Solved

hello everyone!

Here is the completed search using multiple criteria with wildcards (NOT HARDCODING SQL) just in access queries:)

This MS Access help center is great. im loving it!

Attachment below
 

Attachments

Users who are viewing this thread

Back
Top Bottom