Solved

Get value of checked item in listbox

Posted on 2012-03-21
8
297 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
Comment Utility
<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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:ssmith94015
Comment Utility
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
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)

 
LVL 74

Accepted Solution

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

Expert Comment

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

Expert Comment

by:Natchiket
Comment Utility
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
Comment Utility
Yes, first one did work.

Sandra
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now