listbox values moved to text box

G

Greg

I have a listbox that with the MultiSelect setting at 'Simple'. I would like
to have a textbox populated based on the listbox options selected.

For example, listbox contains the following entries: John Jerry Jim.
I would like the textbox to read John, Jerry, Jim.

I can't get this dbl click event to work.

Thanks in advance for your help.....
 
W

Wayne Morgan

Do you want this to happen when you double click the listbox or when the
selection in the listbox changes? If you just want to do it when the user
changes the selection, use the listbox's AfterUpdate event.

Example:
Dim varItem As Variant, strSelections As String, ctl As Control
Set ctl = Me.lstMyListbox
For Each varItem In ctl.ItemsSelected
strSelections = strSelections & ctl.ItemData(varItem) & ","
Next
'Remove the last comma and send the string to the textbox
Me.txtMyTextbox = Left(strSelections, Len(strSelections) - 1)
Set ctl = Nothing
 
G

Greg

Wayne,

Thank you very much for your help.....

One question...when I try to deselect all options, I get a message reading
'Invalid procedure call and argument'

This piece of the code is highlighted....

Me.txtMyTextbox = Left(strSelections, Len(strSelections) - 1)

Any thoughts?

Thanks,

Greg
 
W

Wayne Morgan

When you remove everything, strSelections has zero length because the loop
never runs to give it a value. The -1 is now causing a problem.

Two ways around this:

1) Assign a single character value to the strSelections prior to the loop
(i.e. strSelections = ","). If the loop runs, it will overwrite it. If the
loop doesn't run, the Left() function will remove it.

2) Use an If statement around the Left() function.

If Len(strSelections) <> 0 Then
Me.txtMyTextbox = Left(strSelections, Len(strSelections) - 1)
Else
Me.txtMyTextbox = strSelections
End If
 
Top