You could filter a form so that it showed only the records that contain at
least one of the words.
This example assumes your form has an unbound text box named txtKeywords.
The user types the words, with a space between them. When they move
somewhere else, the form is filtered so that the only records shown are
those that have one of these words in a field named Notes.
It saves any edits before applying the filter. If the user clears the box
(or types nothing but spaces), the form shows all records again.
The code works by splitting the words into a variant array, and then looping
through the array elements to build up the Where string, adding OR to each
one. It then chops off the trailing OR, and applies it to the form's filter.
The current version of Access copes with up to 99 ANDs in the WHERE clause,
so the code checks the user did not type more than 99 words.
Private Sub txtKeywords_AfterUpdate()
Dim strWhere As String
Dim strWord As String
Dim varKeywords As Variant 'Array of keywords.
Dim i As Integer
Dim lngLen As Long
If Me.Dirty Then 'Save first.
Me.Dirty = False
End If
If IsNull(Me.txtKeywords) Then 'Show all if blank.
If Me.FilterOn Then
Me.FilterOn = False
End If
Else
varKeywords = Split(Me.txtKeywords, " ")
If UBound(varKeywords) >= 99 Then '99 words max.
MsgBox "Too many words."
Else
'Build up the Where string from the array.
For i = LBound(varKeywords) To UBound(varKeywords)
strWord = Trim$(varKeywords(i))
If strWord <> vbNullString Then
strWhere = strWhere & "([Notes] Like ""*" & _
strWord & "*"") OR "
End If
Next
lngLen = Len(strWhere) - 4 'Without trailing " OR ".
If lngLen > 0 Then
Me.Filter = Left(strWhere, lngLen)
Me.FilterOn = True
Else
Me.FilterOn = False
End If
End If
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
kbrad said:
I want to take the words typed into a field on a form, search for all of
them
in a field on a table then return a list of records (where any of the
words
match) as a list so that the required record can be selected (like a combo
box).
Can this be done? If so, can anyone help?