473,396 Members | 1,689 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,396 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 11877
NeoPa
32,556 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,556 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,556 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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.