468,554 Members | 1,340 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

Search by Multiple Keywords

72
Hi Everyone,

I want to search records by typing in multiple keywords. I currently have a search form. It has a combo box, text box, Search command button, and a subform. The combo box lists the names of the fields found in my subform. The search form is supposed to allow a user to choose which field he/she wants to search by and then type a keyword(s) in the text box. The subform should display the filtered results.

My problem occurs when I try to use multiple keywords.

For instance, I have a field called KEYWORD in the combo box. In my table, the KEYWORD field could contain only one keyword (like "Distillation") OR it could have multiple keywords (like "Distillation, Crystalization, Vaporization") for each record. In other words, each record has either one keyword or many keywords.

I've been able to write code that searches by single keywords. I can also type in a string of keywords to get a result, BUT they have to be written in the same order exactly as they are found in the table. For instance, using my previous example, I could type in "Distillation, Crystalization" and I would get a result. But if I type "Distillation, Vaporization", I don't get any results b/c that string is not recognized.

How do I get around this problem?

My search code is found below:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2.     If Len(cboSearchField) = 0 Or IsNull(cboSearchField) = True Then
  3.         MsgBox "You must select a field to search."
  4.     ElseIf Len(txtSearchString) = 0 Or IsNull(txtSearchString) = True Then
  5.         MsgBox "You must enter a search string."
  6.     Else
  7.         'Generate search criteria
  8.         GCriteria = cboSearchField.Value & " LIKE '*" & txtSearchString & "*'"
  9.  
  10.         'Filter frmReferenceBooks based on search criteria
  11.         Form_frmReferenceBooks.RecordSource = "select * from tblReferenceBooks where " & GCriteria
  12.         Form_frmReferenceBooks.Caption = "tblReferenceBooks (" & cboSearchField.Value & " contains '*" & txtSearchString & "*')"
  13.  
  14.         MsgBox "Results have been filtered."
  15.     End If
  16. End Sub  
Thanks!
~mforema
Jul 2 '07 #1
5 10547
NeoPa
32,101 Expert Mod 16PB
Try something like :
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2.     Dim intIdx As Integer
  3.     Dim strWork As String, strSearch As String
  4.     Dim GCriteria As String    'Unnecessary if Dimensioned elsewhere
  5.  
  6.     If Nz(cboSearchField, "") = "" Then
  7.         MsgBox "You must select a field to search."
  8.     ElseIf Nz(txtSearchString, "") = "" Then
  9.         MsgBox "You must enter a search string."
  10.     Else
  11.         'Generate search criteria
  12.         GCriteria = ""
  13.         strSearch = txtSearchString
  14.         Do
  15.             intIdx = InStr(1, strSearch, ",")
  16.             If intIdx > 0 Then
  17.                 strWork = Trim(Left(strSearch, intIdx - 1)
  18.                 strSearch = Mid(strSearch, intIdx + 1)
  19.             Else
  20.                 strWork = Trim(strSearch)
  21.                 strSearch = ""
  22.             End If
  23.             GCriteria = GCriteria & " AND ([" & cboSearchField & _
  24.                         "] Like '*" & strWork & "*')"
  25.         Loop While strSearch > ""
  26.         GCriteria = Mid(GCriteria, 6)
  27.         With Me   'Assuming Me = Form_frmReferenceBooks otherwise With Form_frmReferenceBooks
  28.             'Filter frmReferenceBooks based on search criteria
  29.             .RecordSource = "SELECT * " & _
  30.                             "FROM tblReferenceBooks " & _
  31.                             "WHERE " & GCriteria
  32.             .Caption = "tblReferenceBooks (" & _
  33.                        cboSearchField & _
  34.                        " contains '" & _
  35.                        txtSearchString & "')"
  36.         End With
  37.         MsgBox "Results have been filtered."
  38.     End If
  39. End Sub
Jul 2 '07 #2
NeoPa
32,101 Expert Mod 16PB
NB. I removed the wildcards (*) from the .Caption, as that would not read very intelligently for multiple keywords.
Jul 2 '07 #3
FishVal
2,653 Expert 2GB
Hi Everyone,

I want to search records by typing in multiple keywords. I currently have a search form. It has a combo box, text box, Search command button, and a subform. The combo box lists the names of the fields found in my subform. The search form is supposed to allow a user to choose which field he/she wants to search by and then type a keyword(s) in the text box. The subform should display the filtered results.

My problem occurs when I try to use multiple keywords.

For instance, I have a field called KEYWORD in the combo box. In my table, the KEYWORD field could contain only one keyword (like "Distillation") OR it could have multiple keywords (like "Distillation, Crystalization, Vaporization") for each record. In other words, each record has either one keyword or many keywords.

I've been able to write code that searches by single keywords. I can also type in a string of keywords to get a result, BUT they have to be written in the same order exactly as they are found in the table. For instance, using my previous example, I could type in "Distillation, Crystalization" and I would get a result. But if I type "Distillation, Vaporization", I don't get any results b/c that string is not recognized.

How do I get around this problem?

My search code is found below:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2. If Len(cboSearchField) = 0 Or IsNull(cboSearchField) = True Then
  3. MsgBox "You must select a field to search."
  4. ElseIf Len(txtSearchString) = 0 Or IsNull(txtSearchString) = True Then
  5. MsgBox "You must enter a search string."
  6. Else
  7. 'Generate search criteria
  8. GCriteria = cboSearchField.Value & " LIKE '*" & txtSearchString & "*'"
  9.  
  10. 'Filter frmReferenceBooks based on search criteria
  11. Form_frmReferenceBooks.RecordSource = "select * from tblReferenceBooks where " & GCriteria
  12. Form_frmReferenceBooks.Caption = "tblReferenceBooks (" & cboSearchField.Value & " contains '*" & txtSearchString & "*')"
  13.  
  14. MsgBox "Results have been filtered."
  15. End If
  16. End Sub 
Thanks!
~mforema
Hi!

As far as I've got it you want your search criteria evaluate to True if all keywords provided as comma delimited string (txtSearchString) are found in text type field no matter in what order they appear.

There are at least two ways.

1. Split txtSearchString into separate keywords and generate criteria like
InStr(,[Field],keyword1)<>0 AND InStr(,[Field],keyword1)<>0 AND ... AND InStr(,[Field],keywordN)<>0

2. Write VBA function performing desired string comparisson to use it in SQL expression.

Expand|Select|Wrap|Line Numbers
  1. Public Function KeyWordsInStr(ByVal strKeyWords As String, _
  2. ByVal varField As Variant) As Boolean
  3. Dim intPos As Integer
  4. Dim strKeyWord As String
  5.  
  6. KeyWordsInStr = False
  7.  
  8. Do
  9. intPos = InStr(1, strKeyWords, ",")
  10. If intPos = 0 Then
  11. strKeyWord = RTrim(strKeyWords)
  12. Else
  13. strKeyWord = Left(strKeyWords, intPos - 1)
  14. strKeyWords = LTrim(Mid(strKeyWords, intPos + 1))
  15. End If
  16. If Nz(InStr(1, varField, strKeyWord)) = 0 Then Exit Function
  17. Loop Until intPos = 0
  18.  
  19. KeyWordsInStr = True
  20. End Function
  21.  
Jul 2 '07 #4
mforema
72
Try something like :
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2.     Dim intIdx As Integer
  3.     Dim strWork As String, strSearch As String
  4.     Dim GCriteria As String    'Unnecessary if Dimensioned elsewhere
  5.  
  6.     If Nz(cboSearchField, "") = "" Then
  7.         MsgBox "You must select a field to search."
  8.     ElseIf Nz(txtSearchString, "") = "" Then
  9.         MsgBox "You must enter a search string."
  10.     Else
  11.         'Generate search criteria
  12.         GCriteria = ""
  13.         strSearch = txtSearchString
  14.         Do
  15.             intIdx = InStr(1, strSearch, ",")
  16.             If intIdx > 0 Then
  17.                 strWork = Trim(Left(strSearch, intIdx - 1)
  18.                 strSearch = Mid(strSearch, intIdx + 1)
  19.             Else
  20.                 strWork = Trim(strSearch)
  21.                 strSearch = ""
  22.             End If
  23.             GCriteria = GCriteria & " AND ([" & cboSearchField & _
  24.                         "] Like '*" & strWork & "*')"
  25.         Loop While strSearch > ""
  26.         GCriteria = Mid(GCriteria, 6)
  27.         With Me   'Assuming Me = Form_frmReferenceBooks otherwise With Form_frmReferenceBooks
  28.             'Filter frmReferenceBooks based on search criteria
  29.             .RecordSource = "SELECT * " & _
  30.                             "FROM tblReferenceBooks " & _
  31.                             "WHERE " & GCriteria
  32.             .Caption = "tblReferenceBooks (" & _
  33.                        cboSearchField & _
  34.                        " contains '" & _
  35.                        txtSearchString & "')"
  36.         End With
  37.         MsgBox "Results have been filtered."
  38.     End If
  39. End Sub
Thanks! It worked perfectly!
Jul 3 '07 #5
NeoPa
32,101 Expert Mod 16PB
A pleasure, and thanks for letting us know :)
Jul 3 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by huzz | last post: by
83 posts views Thread by D. Dante Lorenso | last post: by
32 posts views Thread by tshad | last post: by
18 posts views Thread by luke noob | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.