If there are several criteria and they are not always used, I like to
construct a Where string and then use that string in opening the query, form
or report for which the criteria apply. I usually use a user-defined
function like GeteWhereString() to return the string:
-----------------
GetWhereString() as String
Dim strWhere as String
strWhere = vbNullString
' FIRST NAME
If Not IsNull(Me!txtPersFirstName) Then
strWhere = strWhere & " AND [PersFirstName] Like " & Chr(34) &
Me![txtPersFirstName] & Chr(34)
End If
' LAST NAME
If Not IsNull(Me!txtPersLastName) Then
strWhere = strWhere & " AND [PersLastName] Like " & Chr(34) &
Me![txtPersLastName] & Chr(34)
End If
' STREET NAME
If Not IsNull(Me!txtPersStreetAddress) Then
strWhere = strWhere & " AND [PersStreetAddress] Like " & Chr(34)
& Me![txtPersStreetAddress] & Chr(34)
End If
' CITY
If Not IsNull(Me!txtPersCity) Then
strWhere = strWhere & " AND [PersCity] Like " & Chr(34) &
Me![txtPersCity] & Chr(34)
End If
If strWhere <> vbNullString then strWhere = Mid(strWhere,6) ' remove
initial " AND "
GetWhereString = strWhere
--------------
In the example above, if the user supplies criteria for any of the items
listed, they will be included in the Where string. If the user leaves some
out (no First Name, for example) the Where string simply ignores that field.
Using "Like" enables the user to use wild cards; the Chr(34)s are just text
delimiters.
This would be used in opening a form like this, assuming the form is
based on a table or query that includes the required fields.
DoCmd.OpenForm "MyForm", , , GetWhereString
If you want to use it with a query, you have to modify the querydef's
SQL property, so it's probably simpler to create a separate form in
datasheet view and open that.
"Matthew" <ma************@eaton.com> wrote in message
news:7f**************************@posting.google.c om...
I am trying to build a form that has certain criteria on it (combo
boxes) which a user can pick from a range of values to specify the
criteria he wants when he runs a query on the form. I am trying out
different ways to do this, but nothing is working. Does anyone know
the easiest way to do this?