问题
How do I select multiple items in the list box, then refer to the Items I have selected?
回答1:
You will need to use a variation of the following steps:
create a list box on a form
populate the list box using the row source.
- 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.
回答2:
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
来源:https://stackoverflow.com/questions/42495257/refer-to-multiple-selected-items-in-a-listbox-in-ms-access