Search code examples
vbams-accesslistbox

How to save as string each item selected in listbox


I have a listbox with 5 items (see screenshot). For each item selected, I want it to be saved as string so I can use them down below my code. I have a loop but it puts the selected items separated by a comma. I want individual strings though. How can this loop be modified to save each varItem as string as it loops?

Dim strParameters, varItem, strComma as string
strComma = ","
For Each varItem In Me!LstParameters.ItemsSelected
        strParameters = strParameters & Me!LstParameters.ItemData(varItem)
        strParameters = strParameters & strComma
        Next varItem
        strParameters = CStr(Left$(strParameters, Len(strParameters) - 1))

Here is my listbox. I want the 2 selected items to be individually in a string.

enter image description here


Solution

  • Use a Collection or a Dictionary which allow referencing items by a key name - an Array does not. Example with Collection, a Dictionary would be basically same.

    Sub BuildCollection()
    Dim colS As Collection, varItem As Variant
    Set colS = New Collection
    'Build 5-item collection
    colS.Add Null, "strRiverDepth"
    colS.Add Null, "strRiverFlow"
    colS.Add Null, "strTurbidity"
    colS.Add Null, "strVolume"
    colS.Add Null, "strSomething"
    'populate collection items with listbox selections
    For Each varItem In Me.LstParameters.ItemsSelected
        colS("str" & Me.LstParameters.ItemData(varItem)) = Me.LstParameters.ItemData(varItem)
    Next varItem
    'do something with collection items
    Debug.Print colS("strRiverDepth")
    Debug.Print colS("strRiverFlow")
    Debug.Print colS("strTurbidity")
    Debug.Print colS("strVolume")
    Debug.Print colS("strSomething")
    End Sub
    

    Debug output of selection example will be like:
    RiverDepth
    RiverFlow
    Null
    Null
    Null

    It is not necessary to pre-build collection. Could instead use colS.Add within listbox loop. Collection will then only have number of items that were selected.
    colS.Add Me.LstParameters.ItemData(varItem), "str" & Me.LstParameters.ItemData(varItem)

    Alternative is to declare 5 variables and then within listbox loop have a Select Case structure to determine which variable to populate.

    strItem = Me.LstParameters.ItemData(varItem)
    Select Case strItem
        Case "RiverDepth"
             strRiverDepth = strItem
        Case "RiverFlow"
             strRiverFlow = strItem
        Case "Turbidity"
             strTurbidity = strItem
        Case "Volume"
             strVolume = strItem
        Case "Something"
             strSomething = strItem
    End Select
    

    And no, cannot dynamically construct variable name. Cannot do: "str" & strItem = strItem

    If you want collection or variables available to other procedures or modules, declare them as Public/Global in module header. Then in whatever procedure uses these entities to accomplish something, will probably want to clear them.

    Also, TempVars are another option. Again, would need to instantiate and set 5, similarly as done for variables.