View Full Version : Search String using spaces as "Or"


themiddlenumber
09-29-2006, 12:36 PM
Friends,

I'm using a search string to filter a continuous form. In an attempt to duplicate "Google", I'd like to use a single search field for multiple criteria.

Is there a way to use the space bar as an "OR" event in my search string?

example: my fat head
would look like: "My" OR "Fat" OR "Head"

I presently use code which strings together multiple search fields with an AND. Here's what I've been using:


Dim strWhere As String
Dim lngLen As Long

If Not IsNull(Me.txtLocation) Then
strWhere = strWhere & "([Location] Like ""*" & Me.txtLocation & "*"") AND "
End IF
If lngLen <= 0 Then
strWhere = Left$(strWhere, lngLen)
Forms!frm_SearchSortEvents.filter = strWhere
Forms!frm_SearchSortEvents.FilterOn = True
End If


End Sub

Thanks for you input!

WayneRyan
09-29-2006, 12:58 PM
tmn,

You're probably gonna have to do something like:


Dim varArray As Variant
Dim i as Long

varArray = Split(Me.txtLocation, " ")

strWhere = "[Location] Like ""*" & varArray(0) & "*"""

For i = 0 to 100
strWhere = strWhere & " Or [Location] Like ""*" & varArray(0) & "*"""
Next i


The above code needs error handling for when you reach the end of the
varArray, but it should get you started.

Wayne

themiddlenumber
09-30-2006, 12:27 PM
Thanks for the code! It sort of works, but I'm still puzzled.

When I enter two words in my search box, the string only sees one. For example, when I search by the criteria "airline s", the code below:

Dim varArray As Variant
Dim i As Long

varArray = Split(Me.txtDescription1, " ")
strWhere = "[Description] Like ""*" & varArray(0) & "*"""

For i = 0 To 2
strWhere = strWhere & " Or [Description] Like ""*" & varArray(0) & "*"""
Next i

Forms!frm_SearchSortEvents.filter = strWhere
Forms!frm_SearchSortEvents.FilterOn = True

End Sub


The code above, yields this filter:
[Description] like "*airline*" OR [Description] Like "*airline*" OR [Description]....(etc)...

I can alter the code to add a search of varArray(1) as well, which is fun. that means both words would be searched. The filter now looks like this:

[Description] like "*airline*" OR [Description] Like "*s*"....

So long as the user enters two words, that's great! But when there's only one word, the code bugs out, and I can't figure out how to fix it.

To sum up: is there a way to ignore the varArray(1) when it's null?




tmn,

You're probably gonna have to do something like:


Dim varArray As Variant
Dim i as Long

varArray = Split(Me.txtLocation, " ")

strWhere = "[Location] Like ""*" & varArray(0) & "*"""

For i = 0 to 100
strWhere = strWhere & " Or [Location] Like ""*" & varArray(0) & "*"""
Next i


The above code needs error handling for when you reach the end of the
varArray, but it should get you started.

Wayne