SteveL13
asked on
How populate text box with selections from a multi-select (simple) listbox?
I have a textbox on a form named txtSortBySelection . Also on the form is a listbox named listSortBy.
When I click on a selection in the listbox I want that selection to appear in the textbox. But when I click on another selection in the listbox I want the 1st selection to remain in the textbox but follow it with a comma and then a space and then the 2nd selection from the listbox. In other words the listbox will display the selections from the listbox separated by a comma and a space in the order they were selected in the listbox.
I have this code but it isn't doing the job...
When I click on a selection in the listbox I want that selection to appear in the textbox. But when I click on another selection in the listbox I want the 1st selection to remain in the textbox but follow it with a comma and then a space and then the 2nd selection from the listbox. In other words the listbox will display the selections from the listbox separated by a comma and a space in the order they were selected in the listbox.
I have this code but it isn't doing the job...
Private Sub listSortBy_AfterUpdate()
With Me.listSortBy
If .Selected(.ListIndex) = True Then
If listSortBy = "" Then
listSortBy = .Column(0, .ListIndex)
Else
listSortBy = listSortBy & "," & .Column(0, .ListIndex)
End If
Else
If InStr(listSortBy, ",") > 0 Then
If InStr(listSortBy, .Column(0, .ListIndex)) = 1 Then
listSortBy = Replace(listSortBy, .Column(0, .ListIndex) & ",", "")
Else
listSortBy = Replace(listSortBy, "," & .Column(0, .ListIndex), "")
End If
Else
listSortBy = ""
End If
End If
End With
Me.txtSortBySelection = listSortBy
End Sub
This may be trickier than you might think...
The kicker here is that if you select the items like this: (Down in sequence)
1
3
5
7
...everything is fine, ...you get: 1, 3, 5, 7
However, if you do this:
1
3
5
7
2
...you will end up with this:
1, 2, 3, 5, 7
(see the attached sample)
This is because the listbox will read the list of selected items in the order they appear in the listboxes internal array.
I mean, you could do it the way you ask, ...but the code will need to be more complex...
JeffCoachman
The kicker here is that if you select the items like this: (Down in sequence)
1
3
5
7
...everything is fine, ...you get: 1, 3, 5, 7
However, if you do this:
1
3
5
7
2
...you will end up with this:
1, 2, 3, 5, 7
(see the attached sample)
This is because the listbox will read the list of selected items in the order they appear in the listboxes internal array.
I mean, you could do it the way you ask, ...but the code will need to be more complex...
JeffCoachman
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks SteveL13
;-)
Enjoy the weekend,
Glad I could help.
;-)
Enjoy the weekend,
Glad I could help.
http://www.helenfeddema.com/Files/accarch220.zip
and here is some code for working with the ItemsSelected collection of a multi-select listbox; you should be able to combine these techniques to get what you want.
Open in new window
Another possibility is to write the items from the listbox to a temp table, and display it in a datasheet subform.