VB ms access listbox and displaying selected item

Posted on 2009-05-04
Last Modified: 2013-12-20
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"


              msgbox "not selected"


        End If

    Next x

Open in new window

Question by:sydneyguy
  • 4
  • 4
  • 2
  • +1
LVL 84
ID: 24293699
You can refer to them as such


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
LVL 33

Expert Comment

ID: 24293743
lstTest.ItemsSelected.Item(x) will give you the value from the x value selected in the listbox.
LVL 33

Assisted Solution

jppinto earned 150 total points
ID: 24293755
Or you could do something like this that will give you a string with all the values selected separeted by a ",".
With Me.List23

        If .ItemsSelected.Count > 0 Then

            For Each I In .ItemsSelected

                MyString = ItemData(I) & Chr(39) & ", " & MyString

            Next I

        End If

End With

Open in new window


Author Comment

ID: 24293766
List23.ItemsSelected.Item (x)
throws up a " invalid use of property "  ?????
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 300 total points
ID: 24293793
Actually mine should read

List23.ItemsSelected(0, x)

See her for more info on the ItemsSelected property:
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.


Accepted Solution

sydneyguy earned 0 total points
ID: 24293797
With Me.List23
        If .ItemsSelected.Count > 0 Then
            For Each i In .ItemsSelected
                MyString = ItemData(i) & Chr(39) & ", " & MyString
            Next i
        End If
End With

does not run but after I
dim an array for ItemData(i)  it  now throws up ItemData(i)  type mismatched
LVL 84
ID: 24293824
ItemData is a property of your listbox; you must tell Access what to use:


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

Author Comment

ID: 24293844

    For i = 0 To List23.ListCount - 1

        If List23.Selected(i) = True Then
            MsgBox List23.Column(0, i)
            lstItem = lstItem & "," '& List1.List(x)
        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
LVL 84
ID: 24293885
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.

Author Comment

ID: 24312493
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
LVL 31

Expert Comment

ID: 24963923
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


Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format vertical text in Access 2016 3 31
Library not Registered 16 42
VB6 ListBox Question 4 32
append to an ms access field 6 20
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now