retrieving listbox values

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Ok so I am working on a userform that contains a listbox with 3 columns in it (bound column = 0). I'm filling this listbox from a database using .additem. When I attempt the retrieve the first columns selected value I am unable to do so. The MultiSelect property of the listbox is set to: 0 - fmMultiSelectSingle Hoping someone understands this better than I.

My Attempt:
Code:
Private Sub clientList_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

Dim myClientID As String

' I tried each of the 3 methods below with no luck
myClientID = clientList.List(clientList.ListIndex, 0)         ' GIVES ME:  Run time error 381: Could not get the List property.  Invalid property array index.
myClientID = clientList.Value                                     ' GIVES ME:  Run time error 94:  Invalid use of Null
myClientID = clientList.Column(0, clientList.RowSource) ' GIVES ME:  Run time error 381: Could not get the List property.  Invalid property array index.

MsgBox myClientID

End Sub
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,166
This should work...

myClientID = clientList.List(clientList.ListIndex, 0)

The error may occur when nothing is selected in the list. This would test if anything is selected.

Code:
    [color=darkblue]If[/color] clientList.ListIndex > -1 [color=darkblue]Then[/color]
        myClientID = clientList.List(clientList.ListIndex, 0)
    [color=darkblue]Else[/color]
        MsgBox "Nothing selected"
    [color=darkblue]End[/color] [color=darkblue]If[/color]
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
If it's just one value you want then you can use the Value property, provided the column which contains the value is the BoundColumn of the listbox.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,776
This is slightly off topic, but I noticed that .BoundColumn is usually a 1 based property.
When .BoundColumn = 0, the .Value property returns the same as the .ListIndex
 

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
Thank you....def a retard...lol The first part of my code resets the list. so that why I could get the selected value....WOW
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,362
Office Version
365
Platform
Windows
I've done that plenty of times.:banghead:
 

Forum statistics

Threads
1,081,560
Messages
5,359,608
Members
400,538
Latest member
leon_oscar

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top