Home > OS >  save value of selected item in listbox with VBA
save value of selected item in listbox with VBA

Time:11-13

I've a problem who drive me crazy. I'm working in a VBA script with Access, and i need to save in a variable the value of list box. At the moment was able just save the the item list order (starting from 0), but i want to save the highlighted line value instead. how I can reach this? Thank you so much, Regards, Riccardo

CodePudding user response:

You will need to loop to get the selected items.

Dim item As Variant

For Each item In YourListBox.ItemsSelected
    MsgBox YourListBox.ItemData(item)
Next

To store the selected items in an array:

Dim arr() As Variant, item As Variant

With YourListBox

    ReDim arr(.ItemsSelected.Count - 1)
    
    For Each item In .ItemsSelected
        arr(item - 1) = .ItemData(item)
    Next
    
End With

You can also flatten the array with the Join function.

Dim s As String
s = Join(arr, ",") 

Debug.Print s
  • Related