|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
ListBox.ItemsSelected Property (Access)
You can use the ItemsSelected property to return a read-only reference to the hidden ItemsSelected collection. This hidden collection can be used to access data in the selected rows of a multiselect list box control.
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 property to retrieve data from selected rows in a list box or combo box. You can list the ItemsSelected collection by using the For Each...Next statement.
For example, if you have an Employees list box on a form, you can list the ItemsSelected collection and use the control's ItemData property to return the value of the bound column for each selected row in the list box.
To enable multiple selection of rows in a list box, set the control's MultiSelect property to Simple or Extended.
The ItemsSelected collection has no methods and two properties, the Count and Item properties.
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 varItm End Sub
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.