473,224 Members | 1,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

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 11837
NeoPa
32,554 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,554 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,554 Expert Mod 16PB
A pleasure, and thanks for letting us know :)
Jul 3 '07 #6

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

Similar topics

3
by: huzz | last post by:
I've a table filed holds keywords seperated by comma, and a web search form where a user can type one of multiple keywords in the search text field seperated by comma aswell. How do i create a sql...
83
by: D. Dante Lorenso | last post by:
Trying to use the 'search' in the docs section of PostgreSQL.org is extremely SLOW. Considering this is a website for a database and databases are supposed to be good for indexing content, I'd...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
5
by: JP SIngh | last post by:
Hi All This is a complicated one, not for the faint hearted :) :) :) Please help if you can how to achieve this search. We have a freetext search entry box to allow users to search the...
3
by: Richard S | last post by:
CODE: ASP.NET with C# DATABASE: ACCES alright, im having a problem, probably a small thing, but i cant figure out, nor find it in any other post, or on the internet realy (probably cuz i wouldnt...
2
by: rlemusic | last post by:
Hi everybody, Iím creating a database in Access (I believe itís 2000) to catalogue items in the archives of a small museum. Iím a total n00b as far as using Access goes, but by looking at some...
0
by: Bob Alston | last post by:
I am doing volunteer work with a human services referral agency. The want me to build a database of referral data - other agencies, sources of information, etc. Ideally it would be a structured...
0
by: Skywick | last post by:
Hi I am trying to do a full text search with a column name for the search term. I can do this using LIKE with: SELECT tblContent.ID FROM tblContent INNER JOIN #keywords ON tblContent.words...
18
by: luke noob | last post by:
can some one please help me adapt my script so that i can search for multiple words within my database, my keywords feild looks something like this.... Big Teddie Bear Collection Tiny Small...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.