Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Get value of checked item in listbox

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
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

<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
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
Avatar of Sandra Smith

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Upload a sample database demonsrating the issue.
List steps to follow and what to expect.
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
Yes, first one did work.

Sandra