wildcard date search

S

shawnpaul

I have a wildcard date search as follows:

Like "*" & [date] & "*"

I am using it since [date] may be a blank field. It returns both 20-Jan-04
(*1/20/2004) and 20-Nov-04 (11/20/2004).

Any suggestions to fix it?

THX
 
A

Allen Browne

Presumably you typed this expression into the Criteria row in query design,
under a Date/Time field, and when the query runs it pops up a dialog and
asks you for the date. And the problem is that if you don't type anything in
the parameter dialog, you want the query to return all records.

There are at least 4 issues to solve here. One is that the Like critieria is
very inefficient, performs string matches, and fails to return records where
the date/time field has no date entry (i.e. the field is null.)

Another is that Date is a reserved word, so not a good name for a parameter.
If you don't mind, we will use What Date instead.

Next is that JET can get the data wrong if you don't declare the parameter.

And finally is trying to handle the null parameter.

To solve all that:

1. Declare the parameter.
In query design view, choose Parameters on the Query menu.
Access opens a dialog. On the first row, enter:
[What Date] Date/Time

2. Change the WHERE clause.
Switch the query to SQL View (View menu.)
Locate the line starting with WHERE.
It will say something like:
WHERE (([Table1].[MyDateField]) Like "*" & [date] & "*")
Change it to:
WHERE (([What Date] Is Null) OR ([Table1].[MyDateField] = [What Date]))

Use your own table name in place of "Table1", and your date/time field in
place of "MyDateField".

This works, because the WHERE clause ultimate evaluates to True or False for
each record. With an OR, it is True if either part is True. If the parameter
is left blank it is Null, so the first part is True so it evaluates to True
for all records. If the parameter is not null, then it is only returned if
the date matches.
 

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