Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching a list box

Status
Not open for further replies.

GenieTR

IS-IT--Management
Jan 21, 2003
28
I have a list box and a text box on a form. The form loads the list box from a table and the text box is used for searching the list box. The code for the text box search is:

On the Change event property:

Private Sub SearchActorTxt_Change()
Dim s as string
Dim i as integer

s = SearchActorTxt.Text

LstAvailable.ListIndex = -1

If SearchActorTxt.Text = " " then
exit sub
end if
For i = 0 to LstAvailable.ListCount - 1
If UCase(LstAvailable.ItemData(i)) Like UCase(s & "*") Then
LstAvailable.ListIndex = i
Exit Sub
end if
Next
end Sub

I am getting a "run-time error 7777" "you're used ListIndex property incorrectly"

The line in question is LstAvailable.ListIndex = -1

can you explain what I am doing wrong.

Thanks in advance
 
.ListIndex is read only. Your actually making this a little harder than necessary.

If you have a text box named txt_SearchString and a Listbox named list_Employees and a button named btn_Search your basic on click code for the button would be.

list_Employees.Value = txt_SearchString.Value

This only works if the values in the listbox are unique. and obviously you'll have to do your own error checking etc... You can also do a list_Employees.Value = list_Employees.Itemdata(0) where 0 is the first item in the listbox. So using your code you would do this.

Private Sub SearchActorTxt_Change()
Dim s as string
Dim i as integer

s = SearchActorTxt.Text

LstAvailable.ListIndex = -1

If SearchActorTxt.Text = " " then
exit sub
end if
For i = 0 to LstAvailable.ListCount - 1
If UCase(LstAvailable.ItemData(i)) Like UCase(s & "*") Then
LstAvailable.Value = LstAvailable.ItemData(i)
Exit Sub
end if
Next
end Sub

Hope this helps you out!
 
Thank you so much, it work like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top