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.

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.
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.Addwithin 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 Casestructure to determine which variable to populate.And no, cannot dynamically construct variable name. Cannot do:
"str" & strItem = strItemIf 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.