search a listbox item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Aditya jha
    New Member
    • Nov 2010
    • 14

    search a listbox item

    hi,

    i hve three list box lst1, lst2 and lst3.

    i hve 4 data in lst1, and 10 data in lst2.
    My question is: if data from lst1 and lst2 data are same then it add data in lst3.( it means i hve to search all data from lst1 into lst2 and found data will be stored in lst3)

    so please help me .....
  • colintis
    Contributor
    • Mar 2010
    • 255

    #2
    I'm not sure if it can be done in the listbox's control source, but I know how it can be done in VBA environment
    Code:
    Private Sub myButton_Click()
    
    Dim myVal As String
    Dim x As Integer, y As Integer
    
    For x = 0 To Me.lst1.ListCount - 1
        myVal = Me.lst1.Column(1, x)
        For y = 0 To Me.lst2.ListCount - 1
            If myVal = Me.lst2.Column(1,y) Then
                lst3.AddItem(myVal)
            End If
        Next y
    Next x
    
    End Sub

    Comment

    • Aditya jha
      New Member
      • Nov 2010
      • 14

      #3
      Again my problem not solved yet,.
      Qestion: I have one Listbox with two column 1 for Name and 2 for ID. I have one listview also which has to 3 column: 1 for Name 2 for ID and 3 for Text box data.
      My Query Is that when I click on add button then it automatically add all items from Listbox to Listview with columnwise. But i want to do if the Name is already in the listview then it add only Text box data with same ID and name. No Name s/b repeated but data may be added???

      Comment

      • ADezii
        Recognized Expert Expert
        • Apr 2006
        • 8834

        #4
        Assuming the Row Source Type of 'ListBox2' is set to Value List, then the following Code will Add every Item in ListBox1 that is not contained in ListBox2.
        Code:
        Dim lst1 As ListBox
        Dim lst2 As ListBox
        Dim intCtr_1 As Integer
        Dim intCtr_2 As Integer
        Dim blnItemFound As Boolean
        
        Set lst1 = Me![ListBox1]
        Set lst2 = Me![ListBox2]
        
        For intCtr_1 = 0 To lst1.ListCount - 1
          blnItemFound = False       'Initialize
            For intCtr_2 = 0 To lst2.ListCount - 1
              If lst1.ItemData(intCtr_1) = lst2.ItemData(intCtr_2) Then
                blnItemFound = True
                  Exit For
              End If
            Next intCtr_2
              If Not blnItemFound Then lst2.AddItem (lst1.ItemData(intCtr_1))
        Next intCtr_1

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          I've merged this answer from another of your threads as it is a good one.

          Next time you decide to repost your question just because the answers do not suit you though, I will suspend your account. Three threads for the same question is totally unacceptable. All you need do is to read the sticky thread at the top of the forum to tell you how to get a question answered. It involves posting a decent question that makes sense in the first place. If none of the answers suit your needs, you can be sure that it's due to the poor nature of the original question.

          DO NOT REPOST YOUR QUESTIONS!

          Comment

          Working...