I use the syntax I mentioned above, which handles both NULLs and ZLS.I read it too fast and didn't notice the &.
Private Sub cmd_Filter_Click
Dim varCriteria as variant
'I use the same button to filter and clear the filter
If cmd_Filter.Caption = "&Clear Filter" then
me.Filter = ""
me.FilterOn = False
me.cmd_Filter.Caption = "&Filter"
Exit Sub
End IF
'Process the filter
if me.cbo_FY & "" <> "" Then
varCriteria = "[Fiscal Year] = '" & me.cbo_FY & "'" 'assumes FY is a string
'varCriteria = "[Fiscal Year] = " & me.cbo_FY 'assumes FY is numeric
endif
if me.cbo_Status & "" <> "" Then
varCriteria = (varCriteria + " AND ") & "[Status] = '" & me.cbo_Status & "'"
endif
if me.txt_Comments & "" <> "" Then
varCriteria = (varCriteria + " AND ") & "[Comments] Like '*" & me.txt_Comments & "*'"
endif
if isnull(varCriteria) = False Then
me.Filter = varCriteria
me.filterOn = true
else
me.FilterON = false
end if
End Sub
This would build a filter string for the form and then apply it based upon the values in those combo boxes. If the combo is left blank as mentioned by PatHartman, you would simply ignore those controls as you build the criteria string. Otherwise, you would add the to the overall criteria.varCriteria = (varCriteria + " AND ") & "[Status] = '" & me.cbo_Status & "'"
would be interpretted as:
varCriteria = (NULL + " AND ") & "[Status] = '" & me.cbo_Status & "'"
then as:
varCriteria = NULL & "[Status] = '" & me.cbo_Status & "'"
and finally as:
varCriteria = "[Status] = '" & me.cbo_Status & "'"
But if cboFY had a value, then:
varCriteria = (varCriteria + " AND ") & "[Status] = '" & me.cbo_Status & "'"
might actually be interpretted as:
varCriteria = "[Fiscal Year] = '2013' AND [Status] = '" & me.cbo_Status & "'"
Then, my query would have SQL that looks like:
SELECT * from yourTable
WHERE [SomeField] = [Forms]![FormName]![comboN
OR [Forms]![FormName]![comboN
But you could use the empty value as the "all values" in which case, the query might look like:
SELECT * from yourTable
WHERE [SomeField] = [Forms]![FormName]![comboN
OR [Forms]![FormName]![comboN
The latter part of this 2nd query makes sure that if the combo is either NULL or an empty string, it will match in the comparison to the empty string.