Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

storing the selected values of a listbox in different variables

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
0
0
IT
Hallo,
I have a listbox with multiselect property. I would like to store the selected items in different variables... but I don't know where to begin. Do you have an hint?

thanks
 
Hi

Whilst you could use seperate vairables to store the selected items storing them in an array would be an easier option.

The code below gives an example of how to do this

Code:
Private Sub StoreInfo()
    Dim varItem As Variant
    Dim intAryCount As Integer
    Dim lstBox1 As ListBox
    Dim aryItems()
    
    Set lstBox1 = Me.List1
    intAryCount = 0
        
    For Each varItem In lstBox1.ItemsSelected
        ReDim Preserve aryItems(intAryCount)
        aryItems(intAryCount) = lstBox1.ItemData(varItem)
        intAryCount = intAryCount + 1
    Next
    
End Sub

If there is some reason why you can only use seperate vairable let me know and I'll have a re-think as to how to do it.

Waud
 
Browse the ItemsSelected collection of the listbox.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
hallo,
thanks to both. My aim is to create a query using the variables, something like:
SQL="SELECT "' variable1 '", "' variable2 '" ....etc"
that's why I was thinking about assigning to each different selected value a different value.
 
Something like this ?
strList = ""
For Each varItem In lstBox1.ItemsSelected
strList = "," & lstBox1.ItemData(varItem)
Next
If strList > "" Then
strSQL = "SELECT " & Mid(strList, 2) & " FROM ..."
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
actually the query is quite complicated and I would need to plit the variables in different part of it. That's why I was thinking about assigning the selected item to different variables.
I was thinking to something like this:

dim listquantity as integer
dim i as integer
listquantity = Me.List0.ListCount

i = 0
Do While i < listquantity
If Me.List0.Selected(0) = True Then
Dim listvalue & i As String
'and then assign the value of me.list0.select(0) to the listvalue & i variable
Else: End If
Loop

But of course "Dim listvalue & i as string" is not something that works... Do you know if there is a workaround to that?
thanks

 
Haven't tested it, but it might work if you put your variables in another combo box (you could set it's visible property to NO). Then, work your way through the second combo box as you assign values to variables.


Randy
 
Use array as Waud suggested you.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top