M
miss031
I made a search box at the top of my form, so users can search by name, phone
number or bidder number, using the code below. If the user types in "71" it
will bring up bidder 71, 710, 711, etc., as well as anyone who has 71 in
their phone number. What I would like is to prioritize the search results,
bringing exact matches to the bidder number to the top of the list. Is this
something I would do in the code, or the query?
Dim StrWhere As String
Dim strWord As String
Dim varKeywords As Variant 'Array of keywords.
Dim i As Integer
Dim lngLen As Long
If Me.Dirty Then 'Save first.
Me.Dirty = False
End If
If IsNull(Me.txt_search_box) Then 'Show all if blank.
Me.Filter = 0
Me.FilterOn = True
Else
varKeywords = Split(Me.txt_search_box, " ")
If UBound(varKeywords) >= 5 Then '99 max ORs.
MsgBox "Too many words."
Else
'Build up the Where string from the array.
For i = LBound(varKeywords) To UBound(varKeywords)
strWord = Trim$(varKeywords(i))
If strWord <> vbNullString Then
StrWhere = StrWhere & "([lastname] Like ""*" & strWord & _
"*"") OR ([contactcompany] Like ""*" & strWord &
"*"") OR ([FirstOfphone_number_] Like ""*" & strWord & "*"") OR
([bidder_number] Like ""*" & strWord & "*"") OR ([seller_number] Like ""*" &
strWord & "*"") OR "
End If
Next
lngLen = Len(StrWhere) - 4 'Without trailing " OR ".
If lngLen > 0 Then
Me.Filter = Left(StrWhere, lngLen)
Me.FilterOn = True
Debug.Print StrWhere
Else
Me.FilterOn = False
End If
End If
End If
number or bidder number, using the code below. If the user types in "71" it
will bring up bidder 71, 710, 711, etc., as well as anyone who has 71 in
their phone number. What I would like is to prioritize the search results,
bringing exact matches to the bidder number to the top of the list. Is this
something I would do in the code, or the query?
Dim StrWhere As String
Dim strWord As String
Dim varKeywords As Variant 'Array of keywords.
Dim i As Integer
Dim lngLen As Long
If Me.Dirty Then 'Save first.
Me.Dirty = False
End If
If IsNull(Me.txt_search_box) Then 'Show all if blank.
Me.Filter = 0
Me.FilterOn = True
Else
varKeywords = Split(Me.txt_search_box, " ")
If UBound(varKeywords) >= 5 Then '99 max ORs.
MsgBox "Too many words."
Else
'Build up the Where string from the array.
For i = LBound(varKeywords) To UBound(varKeywords)
strWord = Trim$(varKeywords(i))
If strWord <> vbNullString Then
StrWhere = StrWhere & "([lastname] Like ""*" & strWord & _
"*"") OR ([contactcompany] Like ""*" & strWord &
"*"") OR ([FirstOfphone_number_] Like ""*" & strWord & "*"") OR
([bidder_number] Like ""*" & strWord & "*"") OR ([seller_number] Like ""*" &
strWord & "*"") OR "
End If
Next
lngLen = Len(StrWhere) - 4 'Without trailing " OR ".
If lngLen > 0 Then
Me.Filter = Left(StrWhere, lngLen)
Me.FilterOn = True
Debug.Print StrWhere
Else
Me.FilterOn = False
End If
End If
End If