问题
I have a search form I creating in access with a code to search for keywords and then create a table with the results:
Like"*"&[FORMS]![Search_Form]![KW_Text]&"*"
WHich basically tells it to read the keyword i type in and pull up any matching results. I would like to be able to type in multiple words, in the table containing all the data I have multiple keywords for each bit of data all separated by comas. So if I type in Manager it returns all results with the word Manager in it, I would like to be able to type in Manager, Supervisor and have it return all results for manager and all results for supervisor.
回答1:
You can use the SPLIT()
function in VBA to split the search string into an array, then For Each
through the array to build up a search/filter string such as
(thing LIKE "*Manager*") OR (thing LIKE "*Supervisor*")
回答2:
I use this code. It create a OR between your sting separated by a space. I put this processed string in an querydefs
Function CreateOr(MyCriteria As String, MyField As String) As String
Dim MyChar As String
Dim MyUniqueCriteria As String
Dim MyFinalCriteria As String
Dim I, j As Integer
j = 0
For I = 1 To Len(MyCriteria)
MyChar = Mid(MyCriteria, I, 1)
If MyChar = " " Then
If j = 0 Then
MyFinalCriteria = MyFinalCriteria & MyField & "=" & MyUniqueCriteria
Else
MyFinalCriteria = MyFinalCriteria & " or " & MyField & "=" & MyUniqueCriteria
End If
MyUniqueCriteria = ""
j = j + 1
Else
MyUniqueCriteria = MyUniqueCriteria & MyChar
End If
Next
CreateOr = MyFinalCriteria
End Function
Hope it help you
来源:https://stackoverflow.com/questions/15973768/parsing-access-code