Daniel L. VanDenBosch
Daniel L. VanDenBosch

Reputation: 2744

Refer to multiple selected Items in a listbox in ms-access

How do I select multiple items in the list box, then refer to the Items I have selected?

Upvotes: 2

Views: 5108

Answers (2)

ASH
ASH

Reputation: 20362

Something like . . .

Private Sub OKButton_Click()
      Dim Msg As String
      Dim i As Integer
      Msg = "You selected" & vbNewLine
      For i = 0 To ListBox1.ListCount - 1
          If ListBox1.Selected(i) Then
              Msg = Msg & ListBox1.List(i) & vbNewLine
          End If
      Next i
      MsgBox Msg
      Unload UserForm1
  End Sub

Upvotes: 0

Daniel L. VanDenBosch
Daniel L. VanDenBosch

Reputation: 2744

You will need to use a variation of the following steps:

  1. create a list box on a form

  2. populate the list box using the row source.

  3. go to the other tab and change the multiselect property to extended

I then used the following VBA

Option Compare Database
Private Item_IDs as string

Private Sub List_item_id_Click()
Dim i As Integer, count As Integer
Dim Item_IDs As String
count = 1
For i = 0 To Me.List_item_id.ListCount - 1
    If Me.List_item_id.Selected(i) = True Then
        Item_IDs = Item_IDs & ", " & Me.List_item_id.ItemData(i)
        count = count + 1
    End If
Next i
Item_IDs = Mid(Item_IDs, 3)
Debug.Print Item_IDs


End Sub

Now every time I click on a value in the list, it will return the a comma separated value string (Item_IDs) of the things I have selected. Use CTRL+G in the VBA window to open the immediate window and see the fruits of your labors.

Upvotes: 3

Related Questions