The ItemsSelected collection provides a way to access data in the selected rows of a list box or combo box.
The ItemsSelected collection is unlike other collections in that it is a collection of Variants rather than of objects. Each Variant is an integer index referring to a selected row in a list box or combo box.
Use the ItemsSelected collection in conjunction with the Column property or the ItemData method to retrieve data from selected rows in a list box or combo box. You can enumerate the ItemsSelected collection using the For Each...Next statement.
For example, if you have an Employees list box on a form, you can enumerate the ItemsSelected collection and use the control’s ItemData method to return the value of the bound column for each selected row in the list box.
Tip To enable multiple selection of rows in a list box or combo box, set the control’s MultiSelect property to Simple or Extended.
The ItemsSelected collection has no methods and one property, the Count property.
Count Property (Microsoft Access).
For Each...Next Statement.
The following example prints the value of the bound column for each selected row in a Names list box on a Contacts form. To try this example, create the list box and set its BoundColumn property as desired and its MultiSelect property to Simple or Extended. Switch to Form view, select several rows in the list box, and run the following code.
Sub BoundData() Dim frm As Form, ctl As Control Dim varItm As Variant Set frm = Forms!Contacts Set ctl = frm!Names For Each varItm In ctl.ItemsSelected Debug.Print ctl.ItemData(varItm) Next varItmSub
The next example uses the same list box control, but prints the values of each column for each selected row in the list box, instead of only the values in the bound column.
Sub AllSelectedData() Dim frm As Form, ctl As Control Dim varItm As Variant, intI As Integer Set frm = Forms!Contacts Set ctl = frm!Names For Each varItm In ctl.ItemsSelected For intI = 0 To ctl.ColumnCount - 1 Debug.Print ctl.Column(intI, varItm) Next intI Debug.Print Next varItm End Sub