Search Form
This is the way I did it:
My example is a search form from which users can search by multiple fields. To simplify, I'll use the fields FirstName and LastName. Each search expression is joined by the AND function. For example, if the user enters John in FirstName and Doe in LastName, clicking the cmdFind button will search for all records with first name John AND last name Doe.
The code also uses the * wildcard at the end of each expression so the user can enter just the first portion of a name. For instance, to find John Doe, the user can use Jo in FirstName and D in LastName.
The resulting combination of the search expressions looks like this: [First] Like 'Jo*' AND [Last] Like 'D*'
This search string is then used in the DoCmd.OpenForm 's Where argument.
I created a form with two unbound fields, FirstName and LastName. I then created a button to perform the find function. The following is the code I used for that button's On Click event(comments have been added to assist in understanding what I have done, remove these from the code):
Dim stLinkCriteria As String
If Not IsNull(FirstName) Then
If stLinkCriteria = "" Then
stLinkCriteria = "[First] Like " & Chr$(39) & Me!FirstName
Else
stLinkCriteria = stLinkCriteria & " AND [First] Like " & Chr$(39) & Me!FirstName
End If
If Right$(Me!FirstName, 1) = "*" Then
stLinkCriteria = stLinkCriteria & Chr$(39)
Else
stLinkCriteria = stLinkCriteria & "*" & Chr$(39)
End If
End If
If Not IsNull(LastName) Then
If stLinkCriteria = "" Then
stLinkCriteria = "[Last] Like " & Chr$(39) & Me!LastName
Else
stLinkCriteria = stLinkCriteria & " AND [Last] Like " & Chr$(39) & Me!LastName
End If
If Right$(Me!LastName, 1) = "*" Then
stLinkCriteria = stLinkCriteria & Chr$(39)
Else
stLinkCriteria = stLinkCriteria & "*" & Chr$(39)
End If
End If
DoCmd.OpenForm "frmMain", , , stLinkCriteria
As you can see, there are 2 blocks of code that are almost identical. You can repeat this as many times as you want for as many search criteria fields you want to use. Just replace the names of the fields. Feel free to e-mail me if you have any questions on how this works.
junk@numberfive.net