Test for OR <parameter> IS NULL. In query design view you'd out something
like this in the criteria row of the Country column:
Forms![YourForm]![txtCountry] OR Forms![YourForm]![txtCountry] IS NULL
In SQL it looks like this:
SELECT *
FROM Customers
WHERE (Country = Forms![YourForm]![txtCountry]
OR Forms![YourForm]![txtCountry] IS NULL);
If the user enters a country name in the parameter then the first half of
the expression will evaluate to TRUE for each row with the relevant country
name, the second half will evaluate to FALSE because he parameter isn't Null.
As an OR operation requires only one part to be TRUE then the row will be
returned. For rows with a different country name both parts will evaluate to
FALSE so those rows won't be returned.
If no value is entered in the parameter then the second half of the
expression will evaluate to TRUE for every row regardless of the country
name, so all rows will be returned.
You'll sometimes see it recommended that you use:
LIKE Forms![YourForm]![txtCountry] & "*".
However, this will not return rows where the Country column is NULL, so
unless Nulls are disallowed in the Country column, i.e. it’s a 'Required'
column, not all rows will necessarily be returned if no value is entered in
the parameter. The above method is more reliable therefore. If you want to
see the difference in results try both methods on the Region column in the
Customers table in the sample Northwind database which comes with Access.
BTW if you save the query in design view and then reopen it in design view
you'll find that Access has moved things around. Don't worry it will work
just the same.
Ken Sheridan
Stafford, England
Emily T said:
How can I set up my criteria so that if a user does not enter a value in the
form, all records are returned?
For example my form allows a user to search by country. But if the country
text box is left blank no records are returned (which I don't want to happen).
Thank you.