Solved

Get value of checked item in listbox

Posted on 2012-03-21
8
301 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
This collection of functions covers all the normal rounding methods of just about any numeric value.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

809 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