Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox - move ID AND description to another listbox 1

Status
Not open for further replies.

cutestuff

Technical User
Sep 7, 2006
162
CA
hi,

I'm looking for suggestions/help on how to do this.
I have 2 listboxes - List1 and List2.
List1 contains a set of data (loaded from a query) with 2 columns - ID and Description.
List2 will contain every item the user selects from List1 but it only shows ID's.

Here's my problem:
I would like List2 to show both ID AND Description when user chooses an item from List1.

Here's the code:
Code:
Private Sub cmdAddItemstoList2_Click()
Dim lst1 As ListBox, lst2 As ListBox
    Dim itm As Variant
    'Dim tmpDescription As String

    
    Set lst1 = Me!List1
    Set lst2 = Me!List2
    ' Check selected items.
  
       For Each itm In lst1.ItemsSelected
             ' Set RowSource property for first selected item.
                If lst2.RowSource = "" Then
                    lst2.RowSource = lst1.ItemData(itm)
                Else
            ' Check whether item has already been copied.
                If Not InStr(lst2.RowSource, lst1.ItemData(itm)) > 0 Then
                    lst2.RowSource = lst2.RowSource & ";" & lst1.ItemData(itm)
                    End If
                End If
        Next itm
End Sub

Thank you in advance.
 
You may try this:
Code:
For Each itm In lst1.ItemsSelected
  If lst2.RowSource = "" Then
    lst2.RowSource = lst1.Column(0, itm) & ";" & lst1.Column(1, itm)
  ElseIf InStr(lst2.RowSource, lst1.ItemData(itm) & ";") = 0 Then
    lst2.RowSource = lst2.RowSource & ";" & lst1.Column(0, itm) & ";" & lst1.Column(1, itm)
  End If
Next itm

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It worked!

Did anyone ever tell you you're a GENIUS? :)

Thanks so much!

If you don't mind, i have another question: my descriptions have commas in them. The code seems to read it and break it apart based on the commas. Do you know how I can ask it to ignore the commas in the description?

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top