Results 1 to 2 of 2

Thread: Problems with the Listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Posts
    5

    Unhappy

    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


  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width