I’m writing some VBA to create a search page in my MS Acccess database and running into some trouble with DoCmd.ApplyFilter in the Search_Click() sub.

My code looks like this
Private Sub Search_Click()
DoCmd.ApplyFilter "", _
"([site] = [Forms]![SWP Search]![txtSite] " & _
" Or IsNull([Forms]![SWP Search]![txtSite])) " & _
"AND " & _
"([asset] = [Forms]![SWP Search]![txtAsset] " & _
" Or IsNull([Forms]![SWP Search]![txtAsset]))"
End Sub
Or in pseudo code…
Shows results where true...
([site column] = txtbox1 OR isnull(txtbox1))
AND
([asset col ] = txtbox2 OR isnull(txtbox2))
Obviously, the desired functionality is as follows…
- Site chosen, Asset blank -> Filter on site only
- Site chosen, Asset chosen -> Filter on both
- Site blank, Asset blank -> Return all rows
- Site blank, Asset chosen -> Filter on asset only
But what’s actually happening is…
- Site chosen, Asset blank -> works
- Site chosen, Asset chosen -> works
- Site blank, Asset blank -> no rows returned
- Site blank, Asset chosen -> no rows returned
So it seems like when Site is blank, IsNull() isn’t evaluating true and so the first part of the filter is FALSE and the thing just quits there and then.
Any idea why?
Try does using
IsEmpty()instead ofIsNull()make difference, ieas the empty txtbox might return empty string, not NULL as you expect. Or compare textbox value against empty string, possibly using
trim()first.