Question : Handling Null values in Microsoft Access query criteria

I have a form that is a search panel of sorts. It has several fields that a user can type information into, then click a Search button which executes a query that uses the form fields as parameters and returns the results.

Because the user can put data in any of the fields, or leave them blank, I'm filtering the criteria in the query using the following code:

Like IIf(IsNull([Forms]![searchForm]![CRField]),"*",[Forms]![searchForm]![CRField])

So if there is anything in the search field on the form, it will search for that. If the search field on the form is blank, it searches for anything.

Of course, we all know that "*" does not include NULL values, which are possible in this database. I had hoped that it would be as easy as adding "Or Is Null" like this:

Like IIf(IsNull([Forms]![searchForm]![CRField]),"*" Or Is Null,[Forms]![searchForm]![CRField])

When I tab out of the criteria field, it immediatly "corrects" the syntax to add another 'Like':

Like IIf(IsNull([Forms]![searchForm]![CRField]),Like "*" Or Is Null,[Forms]![searchForm]![CRField])

Executing this query returns nothing.

In fact, I've simplified it way down to this:

IIf(IsNull([Forms]![F - Search]![CRField]),Is Null)

Thinking that if the Form's search field is blank, it should return all records where the entry is null.  (There are several in that column) However, yet again it returns nothing.

Simply typing in Is Null as the criteriaworks fine -- so somehow the Iff statement is not passing the Is Null the way I expected it to.

Any ideas?

Answer : Handling Null values in Microsoft Access query criteria

Okay, I have it working but only after quite a bit of work.

The above suggestions didn't work, all of them did the same thing that my code did previously: they didn't return records with NULL values.

I left some information out of my origonal question that may have affected your responses, though. (I didn't actually think about this until rpoole's comment suggested going into SQL view: First, I have two tables in a Left Join structure. Second, I have "Total" turned on and most of my fields are Grouped. Because of this, Access is using HAVING clauses instead of WHERE clauses. I'm not familiar enough with Access to fully understand the ramifications of this... but perhaps you do.

So, my solution is to use expressions and critieria to filter things out that I don't want, rather than using WHERE conditions to filter things in that I do want. (Did that make sense?)

For example, I have the following expression added to my query and hidden:
[FSearch] is the form.
[CRField] is the field on the form.
[CR] is the column in the table.

IIf(IsNull([Forms]![FSearch]![CRField]),[CR] Like "*" Or [CR] Is Null,[Forms]![FSearch]![CRField] Like [CR])

Then I have the criteria as <>False

By adding one of these for each field that I am filtering by, my query is incredibly ugly. But, it works perfectly whether or not anything is entered into each of the 12 search boxes on my form.
Random Solutions  
 
programming4us programming4us