Hi,
I have a bound listbox which displays data from query based on user's selection. I would like to add a provision to search any type of data within the listbox and highlight/select if present. For that I have provided a textbox and command button. User can type in any value in the textbox and hit button to find. I get an datatype mismatch error in the Instr function code. Here txtSearch is the textbox and List0 is the listbox.
Code Snippet of commandbutton
-------------------------------
Private Sub cmdSearch_Click()
Dim textToSearch As String
Dim MatchFound As Boolean
Dim I As Integer
MatchFound = False
I = 0
textToSearch = Me.txtSearch.Value
'If IsNull(textToSearch) Then
' Loop until I is greater than or equal to items OR there is a match found
Do
' If text is in item, highlight item and mark match as found
If InStr(Me.List0.ItemData(I), textToSearch, 1) > 0 Then
MatchFound = True
Me.List0.ListIndex = I
End If
I = I + 1
Loop Until (I >= Me.List0.ListCount) Or (MatchFound)
'End If
' If no match was found, deselect
If Not MatchFound Then
Me.List0.ListIndex = -1
End If
Please help.
I have a bound listbox which displays data from query based on user's selection. I would like to add a provision to search any type of data within the listbox and highlight/select if present. For that I have provided a textbox and command button. User can type in any value in the textbox and hit button to find. I get an datatype mismatch error in the Instr function code. Here txtSearch is the textbox and List0 is the listbox.
Code Snippet of commandbutton
-------------------------------
Private Sub cmdSearch_Click()
Dim textToSearch As String
Dim MatchFound As Boolean
Dim I As Integer
MatchFound = False
I = 0
textToSearch = Me.txtSearch.Value
'If IsNull(textToSearch) Then
' Loop until I is greater than or equal to items OR there is a match found
Do
' If text is in item, highlight item and mark match as found
If InStr(Me.List0.ItemData(I), textToSearch, 1) > 0 Then
MatchFound = True
Me.List0.ListIndex = I
End If
I = I + 1
Loop Until (I >= Me.List0.ListCount) Or (MatchFound)
'End If
' If no match was found, deselect
If Not MatchFound Then
Me.List0.ListIndex = -1
End If
Please help.