Filter form

pablavo

Registered User.
Local time
Today, 04:22
Joined
Jun 28, 2007
Messages
189
Hi folks

I have just created an input form to collect the date criteria for a Monthly report. I've used DateSerial function as I only need to collect the Month and year.

I'm working on a new report now and would like to ask for some help.

This report will have multiple filters. For example
Date from & to txtboxes
Project name
Project Code

The problem I have is that the technique I've used in the past does not allow for any of the filters to be empty. It comes back with an error and doesn't display any records.

Sometimes this will be the case. For example I might only want to find records using the Project code filter and this will mean leaving the other filters empy. In other cases I might just want the Dates and the Project Name

Does anyone know the best way to create this kind of filter? I'm sure there is several ways. Unforunately my knowledge of VBA and SQL is quite limited at the moment.

Any help would be great:o
 
Last edited:
Code:
SELECT 
    * 
FROM 
    YourTableName 
WHERE 
    FromDate Like(Nz(FormFromDate,"*")) AND
    ToDate Like(Nz(FormToDate,"*")) AND
    ProjectName Like(Nz(FormProjectName,"'*'")) AND
    ProjectCode Like(Nz(FormProjectCode,"*"))
;

You'll most likely have to play with the formatting a little bit (put in single quotes where needed, etc.), but the important part is to see what I'm doing.

The NZ (Null Zero) function will convert a field that is NULL (In your case, a blank on the form) into whatever you want. I'm converting them to asterisks here, which will return all records, the equivalent of no filter.

I'm also using Like here instead of an equals sign so that the asterisks work. This won't work (will return zero records):

SomeField = "*"

This will return all records:

SomeField Like "*"
 
Thanks Moniker! I actually used a different method. However, I'm going to keep this code and give it a try. I'm new to VBA so it's great for me to have all the methods I can get.:)
 

Users who are viewing this thread

Back
Top Bottom