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

吃可爱长大的小学妹 提交于 2021-02-08 05:17:44

问题


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:

  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.




回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!