Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get value of checked item in listbox

Posted on 2012-03-21
8
Medium Priority
?
309 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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 

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 2000 total points
ID: 37750012
...so then my first code snippet should work, No?
0
 
LVL 31

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

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

772 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