Thursday, May 20, 2010

Access True Wildcard Form Search

Needed to build a searchform in MS Access which will allow searching of one or multiple fields. I know that in Sql, this can be accomplished by using:

SELECT * FROM Table where IsNull([ID],'') LIKE IsNull('%' + @ID + '%','%')

However, Access doesn't recognize this as a valid statement, so back to the drawing board...

Eventually I came up with this solution...

IIf(IsNull([Forms]![Form]![Control]),[FieldName],LIKE "*" & [Forms]![Form]![Control] & "*")

With Form being the Form name, and Control being the Control Name.

In a nutshell, this is saying If IsNull(Form Control) Is True, i.e., If no value was put into the search form for this field, then simply show all items in that field. Otherwise, show all items that are LIKE (Wildcard Search) the search criteria.

Hope this helps someone else...