View Full Version : Search for a string in a listbox - Code in VB .65


anandsbr
12-13-2010, 07:29 AM
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.

anandsbr
12-13-2010, 07:36 AM
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. Just a note that rowsource is 'Select a,b,c,d,e from table a Where a = 3. so need to find SQL data not typed data in the listbox.

boblarson
12-13-2010, 07:39 AM
What in the world is VB .65????

anandsbr
12-13-2010, 07:49 AM
Sorry.. Code written in Visual Basic 6.5

vbaInet
12-13-2010, 07:52 AM
The Instr() parameters are incorrect:

InStr(1, Me.List0.ItemData(I), textToSearch, 1)

anandsbr
12-13-2010, 08:01 AM
First parameter is optional. That's why i did not use.

Syntax is
InStr([start, ]string1, string2[, compare])

vbaInet
12-13-2010, 08:07 AM
You must still put the comma to indicate optional parameters.

missinglinq
12-13-2010, 10:08 AM
Actually, the first parameter Start, is not optional if you want to use the fourth parameter, Compare! Using Compare requires Start.

So

InStr(string1, string2)

works, without the place holding comma, and

InStr([start], string1, string2, [compare])

works, but

InStr(string1, string2, [compare])

doesn't.

Linq ;0)>

vbaInet
12-13-2010, 10:14 AM
Actually, the first parameter Start, is not optional if you want to use the fourth parameter, Compare! Using Compare requires Start.

So

InStr(string1, string2)

works, without the place holding comma, and

InStr([start], string1, string2, [compare])

works, but

InStr(string1, string2, [compare])

doesn't.

Linq ;0)>That's quite correct!