Solved

Get value of checked item in listbox

Posted on 2012-03-21
8
302 Views
Last Modified: 2012-03-22
I need to pass the value of the selected item in a listbox to a procedure - simple enough, but I am testing some code and nothing seems to work.  I tried Microsoft's exmaple, but they use List and VBA throws an error.  I can't even get the below to create a message - well, it creates a message, but the value is always -1.  There is only one column with four items - All, Permanenet, Contractor, Vendor.

Sandra

Private Sub cmdCheck_Click()
Dim x As Integer
Dim msg As String

For x = 0 To Me.lstType.ListCount - 1
    If Me.lstType.Selected(x) Then
        msg = msg & Me.lstType.Selected(x) & vbCrLf
    End If
Next x
MsgBox "Message:" & msg
0
Comment
Question by:ssmith94015
8 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37749787
<I need to pass the value of the selected item in a listbox to a procedure>
Just one value...

(Your code is form multiple selections, ...i think...)

You did not state any details on the listbox....

In any event, this will display the value of the "Third" column of a selected record in a listbox:

    MsgBox Me.YourListBox.Column(2)

(Note here that the third column is Column(2), as a listbox VBA columns are Zero based)

JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37749815
Again, you did not state what column you wanted, ...so try it like this:

Dim x As Integer
Dim msg As String

For x = 0 To Me.lstType.ListCount - 1
    If Me.lstType.Selected(x) Then
        msg = msg & Me.lstType.Column(2) & vbCrLf
    End If
Next x
MsgBox "Message:" & msg

Remembering that a listbox's VBA column references are Zero based)

JeffCoachman
0
 

Author Comment

by:ssmith94015
ID: 37749826
actually, you pointed out something I did not think of.  There is one column with four rows, so the list is
      All
      Permanent
      Contractor
      Vendor

The user can select one or more of these items.  This data is then passed to a query for use in the where clause ("WHERE type IN( LISTBOX SELECTION HERE)") that is held in a procedure in the form module.  As a test, I got the below to work, but I need to figure out how to gets quotes and a comma between values when more than one is selected.

Sandra

For Each varItem In Me.lstType.ItemsSelected 'loops thru all items selected from listbox
        strItem = strItem & Me.lstType.Column(0, varItem)
Next varItem
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:ssmith94015
ID: 37749909
Just spoke with the user and they do NOT want multi-select.  So they can only choose one item at a time, so the challegne is now to determine which of the four choices is the one they made.

Sandra
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 37750012
...so then my first code snippet should work, No?
0
 
LVL 30

Expert Comment

by:hnasr
ID: 37751067
Upload a sample database demonsrating the issue.
List steps to follow and what to expect.
0
 
LVL 17

Expert Comment

by:Natchiket
ID: 37753093
hi ssmith94015, unless it is a multi select list box, the value of the list box itself will be the selected item

e.g.

msgbox Me.lstType.Value
0
 

Author Closing Comment

by:ssmith94015
ID: 37754779
Yes, first one did work.

Sandra
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

679 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