Search code examples
vb6

How to search items on listbox vb6


Hello i have a listbox that is populated with values from sql, this is my code

The population of listbox works

Dim Text As String, query As String, infoList As Variant
Dim MyRsADO As adobd.recordset

query = "select id, name from X where status 1 order by name asc"

Debug.Print = query
Set MyRsADO = mainmodule.DBUTILS.DbConnection.GetRecordseFromSQL(query)

ReDim listItems(0)

set listItems(0) = New C_ListItem
listItems(0).fieldName = "name"
listItems(0).fieldLabel = "name"
listItems(0).spaceAlgn = 60
listItems(0).HeaderLeft = 3050
listItems(0).HeaderWidth = 1200

infoList = listItems()
mainmodule.fillList Me, listbox, MyRsADO, "id", infoList, listbox, -1 False

End Sub

Now the code of searching in the listbox

Dim i As Long
For i=0 to listbox.ListCount - 1
listbox.ListIndex = 1
if listbox.text Like txtSearch.Textt then
msgbox "Found"
exit sub
end if
next i
msgbox "0 results"
end sub

The second code doesnt work only shows 0 results i dont understand what i am doing wrong.


Solution

  • Inside the loop you are setting the ListIndex to 1. This means you are always trying to match on the second item in your list. You can get your code to work by changing the ListIndex like this:

    listbox.ListIndex = i
    

    With this change, your code will search the list top to bottom.