How do you count the number of selected records in a ListBox?

  • Thread starter AbraAbraCadabra
  • Start date
A

AbraAbraCadabra

How do you count the number of selected records in a ListBox?
Also how do you access them?

i tried this, but apparently I'm doing something wrong...
To access the number of records...(didn't work)
MsgBox List0.Selected

And I tried this to access the selected records...
MsgBox List0.Selected(0)

What am I doing wrong?
 
F

fredg

How do you count the number of selected records in a ListBox?
Also how do you access them?

i tried this, but apparently I'm doing something wrong...
To access the number of records...(didn't work)
MsgBox List0.Selected

And I tried this to access the selected records...
MsgBox List0.Selected(0)

What am I doing wrong?

To process a multi-Select List Box, look up the ItemsSelected property
in VBA help. There is an example.
While there, also click on the See Also links for additional List Box
properties.
 
D

Douglas J. Steele

MsgBox List0.ItemsSelected.Count

To access the individual records, you can either use:

Dim lngLoop As Long
Dim strSelected As String

For lngLoop = 0 To (Me.List0.ListCount - 1)
If Me.List0.Selected(intLoop) = True Then
strSelected = strSelected & Me.List0.ItemData(intLoop) & vbCrLf
End If
Next lngLoop

or (a little more efficiently)

Dim strSelected As String
Dim varItem As Variant

For Each varItem In Me.List0.ItemsSelected
strSelected = strSelected & Me.List0.ItemData(varItem) & vbCrLf
Next varItem


The fact that I used Me.List0.ItemData() above means that the bound column
will be returned. If you want a different column than the bound one to be
displayed, you'd use the Column collection. For instance, if the values you
want displayed are in the 2nd column of the listbox, you'd use

strSelected = strSelected & Me.List0.Column(1, varItem) & vbCrLf

(the Column collection starts counting at 0, not 1)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top