-
I am a VB beginner and am having problems with the Listbox.
I have a listbox with ID no's and Names.I want to be able to search for the ID or Name. When the user types in the name or ID into a textbox and clicks on the find button, I want the corresponding ID or name in the Listbox to be selected.
I have spent sooo much time on this and just can't seem to get it working.
Can anybody help me, please
-
The listbox itself only allows you to store the names of things, there isn't a "key" field that you can attach to each of the entires in it.
This means that while you can search by name :
Code:
Function FindByName(Text as string) as Long
Dim lNum as Long
For lNum = 1 to List1.ListCount
If List1(lNum) = Text Then
FindByName = lNum
Exit Function
End If
Next lNum
FindByName = -1
End Function
You can't actually search by code.
The only way you "could" try to fake it would be to use a collection and for each member of the collection you set the "key" value to be the ID code you are after. Then all you need to do is test to see if the ID code exists and then use that in the same function above.
Code:
Dim cTest as string
cTest = ""
On Error Resume Next
cTest = MyCollection(cIdToSearch)
On Error goto 0
If cTest <> "" Then
List1.Index = FindByName(cTest)
End If
Hope it leads you in the right direction.