Can you search a listbox by a textbox value when the multiselect property isn't none?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xraive
    New Member
    • Jun 2009
    • 30

    #1

    Can you search a listbox by a textbox value when the multiselect property isn't none?

    Currently I am able to use a texbox to search through the listbox only when the multiselect property is set to none. But when I change the property to simple or extended my function doesn't work. Please see below
    Function
    Code:
    Public Function CheckListbox(strPass As String, lstPersons As ListBox)
    
    Dim intCurrIndex As Integer
    
    Dim intStringLen As Integer
    
        
    
    intStringLen = Len(strPass)
    
        
    
    For intCurrIndex = 0 To lstPersons.ListCount - 1
    
             If Left$(lstPersons.ItemData(intCurrIndex), intStringLen) = strPass Then
            
        Exit For
    
      End If
    
    Next intCurrIndex
    
        
    
    lstPersons = lstPersons.ItemData(intCurrIndex)
    
     End Function
    Calling the function
    Code:
    Private Sub abSearchtbox_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    
        Case Asc(" ") To Asc("z")
    
          strStringTyped = Me.abSearchtbox.Text & Chr(KeyAscii)
            
         Case 8
             If strStringTyped > "" Then
                strStringTyped = Left$(strStringTyped, Len(strStringTyped) - 1)
            End If
    End Select
    
    
    CheckListbox strStringTyped, Me.allAddressBooklbox
    
    End Sub
    I also tried another method but neither work when the multi select property is not none. I can't search at all.


    Any Ideas??
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Hello xraive, I'm a little confused at to what you are trying to accomplish here. Please explain in detail, providing all relevant information.

    Comment

    • xraive
      New Member
      • Jun 2009
      • 30

      #3
      Sorry Adezii,

      I currently have a listbox and would like to be able to search the records. I have a textbox setup so that when the user enters a key an item matching it in the listbox would be selected. This would be similar to the autoexpand feature on the combo box. I would like to be able to have a user type in a string and the first Item matching that string be selected from the listbox. I hope I made myself a little clear. I can attach a sample db of what I am doing if you like.


      Thanks for your time ADezii

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        This should point you in the right direction:
        Code:
        Private Sub abSearchTBox_KeyPress(KeyAscii As Integer)
        Dim strStringTyped As String
        Dim intCurrIndex As Integer
        Dim intStringLen As Integer
        
        'Clear ALL Selections with each Keystroke
        For intCurrIndex = 0 To allAddressBooklbox.ListCount - 1
          allAddressBooklbox.Selected(intCurrIndex) = False
        Next
        
        strStringTyped = Me.abSearchTBox.Text
        
        Select Case KeyAscii
          Case Asc(" ") To Asc("z")
            strStringTyped = strStringTyped & Chr(KeyAscii)
          Case 8
            If strStringTyped <> "" Then
              strStringTyped = Left$(strStringTyped, Len(strStringTyped) - 1)
            End If
          Case Else
            KeyAscii = 0        'CANCEL the Keystroke?
        End Select
        
        intStringLen = Len(strStringTyped)
          
        For intCurrIndex = 0 To allAddressBooklbox.ListCount - 1
          If Left$(allAddressBooklbox.ItemData(intCurrIndex), intStringLen) = strStringTyped Then
            allAddressBooklbox.Selected(intCurrIndex) = True
              Exit For
          End If
        Next intCurrIndex
        End Sub

        Comment

        Working...