Search for a string in a listbox - Code in VB .65 (1 Viewer)

anandsbr

Registered User.
Local time
Today, 09:22
Joined
Nov 17, 2010
Messages
15
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

Registered User.
Local time
Today, 09:22
Joined
Nov 17, 2010
Messages
15
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

Smeghead
Local time
Today, 06:22
Joined
Jan 12, 2001
Messages
32,059
What in the world is VB .65????
 

vbaInet

AWF VIP
Local time
Today, 14:22
Joined
Jan 22, 2010
Messages
26,374
The Instr() parameters are incorrect:

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

anandsbr

Registered User.
Local time
Today, 09:22
Joined
Nov 17, 2010
Messages
15
First parameter is optional. That's why i did not use.

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

vbaInet

AWF VIP
Local time
Today, 14:22
Joined
Jan 22, 2010
Messages
26,374
You must still put the comma to indicate optional parameters.
 

missinglinq

AWF VIP
Local time
Today, 09:22
Joined
Jun 20, 2003
Messages
6,423
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

AWF VIP
Local time
Today, 14:22
Joined
Jan 22, 2010
Messages
26,374
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!
 

Users who are viewing this thread

Top Bottom