Problem with Listbox search

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a list box and a textbox that I will use to search through my records.
I have created a slip of code that works, but only in the first column. How
can I make it search all the columns?

Here's the code:

Private Sub soktext_Change()
Dim lb As ListBox
Dim i As Integer
Dim textlen As Integer
Dim txt As String

txt = Me.soktext.Text 'name of your
text box
textlen = Len(txt) 'length of the
text in the textbox
Set lb = Me.Oppslist 'name of your
list box
For i = 1 To lb.ListCount 'for every item
in the list
If Left(lb.ItemData(i), textlen) = txt Then 'does it match?
lb.Selected(i) = True 'select the item
Exit For 'don't
bother with the rest
End If
Next
End Sub
 
Sorry about the mess in my code... here's a more readable code (without the
descriptions)


Private Sub soktext_Change()
Dim lb As ListBox
Dim i As Integer
Dim textlen As Integer
Dim txt As String

txt = Me.soktext.Text
textlen = Len(txt)
Set lb = Me.Oppslist
For i = 1 To lb.ListCount
If Left(lb.ItemData(i), textlen) = txt Then
lb.Selected(i) = True
Exit For
End If
Next
End Sub
 
Hi,
Use the Column property instead of ItemData.
Syntax:
control.Column(column, row)

remember that columns are 0 based so your 1st column is 0
 
Hi,
Not unless you tell me what you've tried, how you coded it
and what happens when the code is run.

The Column property is what you would use to get the
value of any column in your listbox.
 
Heres my code:

Private Sub soktext_Change()
Dim lb As ListBox
Dim i As Integer
Dim x As Integer
Dim textlen As Integer
Dim txt As String

txt = Me.soktext.Text
textlen = Len(txt)
Set lb = Me.Oppslist
For x = 0 And i = 0 To lb.ListCount

If Control.Column(x, i) = txt Then
lb.Selected(i) = True
Exit For
End If
Next
End Sub

How do I use the Control.Column?
 
For x = 0 And i = 0 To lb.ListCount

isn't valid syntax.

For i = 0 To (lb.ListCount - 1)
For x = 0 to (lb.ColumnCount - 1)
If Control.Column(x, i) = txt Then
lb.Selected(i) = True
Exit For
End If
Next x
Next i
 
Hi,
Just a small correction:
If lb.Column(x, i) = txt Then

The Control.Column was an excerpt from Help that I initially posted
and was meant to be replaced with the actual listbox name :-)
 
Back
Top