Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

VB ms access listbox and displaying selected item

I have got a list box with multiple select switched on, which is living on an access form and the code below works and can pick between the selected items and the not selected items,

but cannot figure out the syntax to display the item values that have been selected out of the list box

not quite sure what the syntax is

thanks in advance
For x = 0 To List23.ListCount - 1
        If List23.Selected(x) = True Then
                                                                        MsgBox List23.Selected (x) 
 
*********  display in here the syntax to display the value of the listbox  ***********
 
            msgbox "selected"
        else 
              msgbox "not selected"
 
        End If
    Next x

Open in new window

Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

You can refer to them as such

List23.column(x,0)

would show the FIRST column for tha row (columns are zero-based).

Also, you can use the ItemsSelected collection to show ONLY those items that have been selected, without iterating the entire list:

dim var As variant

for each var in List23.ItemsSelected
  '/do something here
Next var
lstTest.ItemsSelected.Item(x) will give you the value from the x value selected in the listbox.
SOLUTION
Avatar of jppinto
jppinto
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sydneyguy

ASKER

List23.ItemsSelected.Item (x)
throws up a " invalid use of property "  ?????
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ItemData is a property of your listbox; you must tell Access what to use:

List23.ItemData(i)

See the link I posted above for syntax and usage of the ItemsSelected property.

    For i = 0 To List23.ListCount - 1

        If List23.Selected(i) = True Then
            MsgBox List23.Column(0, i)
            lstItem = lstItem & "," '& List1.List(x)
         
         Else
         
        End If
    Next i

does the job that will roll through the listbox and allow me to extract the data from the box
thanks every one
Glad this works, but if you get time I'd change this to use the ItemsSelected property of that listbox ... it's nit-picky, but there's just no reason to cycle through every item in that Listbox when the ItemsSelected property is available.
now that I have something that actually works I will refine as per your suggestion, just so frustrating when you know that you have doen it before but just cannot find the code to show how, but now it works I will refine thanks for alll your help
garry
Here is some code for iterating through the ItemsSelected collection of a listbox.  Instead of setting a variable, you could (for example) display the item value in the Immediate Window, write it to a text file, or whatever.  Note that varItem should be declared as a variant, because the items in different listboxes might be of different date types.  You could make it another type depending on what is in your listbox.
   For Each varItem In lst.ItemsSelected
 
      lngID = Nz(lst.Column(0, varItem))
   
   Next varItem

Open in new window