multiple list box selection

  • Thread starter Thread starter Jimbo
  • Start date Start date
J

Jimbo

I need to be able to select multiple items in a list box and store them in a
field. Can this be done? Or can I somehow use a list box with multiple
selections for a given record?

Thanks!
 
Hi Jimbo

List boxes have a 'Selected' property for each item in the list. You could
loop through all the items in the list box, building up a string containing
the values of all the selected items. This string could then be shoved into
your field.

Hope this helps

David
 
Okay. I haven't checked this so if you can't work through the errors give me
a shout.

Let's say your list box is called [List Box], and it has 3 columns in it.
You want to put the value of the third column for all the selected items in
the list box into a string, which I'll call ListString.

Just to complicate things I'll separate all the values with a comma and
space. *

Dim ListString As String
String = ""
Dim SelectedRecord As Variant
For Each SelectedRecord In [List Box].ItemsSelected
String = String + [List Box].Column(2, SelectedRecord) & ", "
Next SelectedRecord

You could then assign the value of ListString to a text box called [Text Box].

[Text Box] = ListString

There are a few properties which make list boxes extremely useful once you
get the hang of them. These are ItemsSelected, Selected, Column and ItemData.
Check 'em out!

Cheers

David

* Removing the final ", " from the end of ListString is a story for another
time. Suffice to say there are two relevant functions, StrLen, which gives
you the number of characters in a string, and Left, which returns the
left-hand part of a string of a specified number of characters in length.
 
I just need to be able to high light the ones in the lstBox, click a button
and have those entered into a text field, perferably in a list fashion. The
table my list is coming from only has one column. Does that make it simplier?

thanks!
 
Hi Troy

If the list box has only the one column, you should be able to use

String = String + [List Box].ItemData(SelectedRecord)

instead of

String = String + [List Box].Column(0, SelectedRecord)

The choice is yours really.

If you want the string you've built up to appear in the text box as a 'list'
(I assume you mean with each item on a separate line) you will need to
separate each item with a line break. So use this:

String = String + [List Box].Column(2, SelectedRecord) & chr(13) & chr(10)

Hope this works for you.

Cheers

David

Troy said:
I just need to be able to high light the ones in the lstBox, click a button
and have those entered into a text field, perferably in a list fashion. The
table my list is coming from only has one column. Does that make it simplier?

thanks!

David Cleave said:
Okay. I haven't checked this so if you can't work through the errors give me
a shout.

Let's say your list box is called [List Box], and it has 3 columns in it.
You want to put the value of the third column for all the selected items in
the list box into a string, which I'll call ListString.

Just to complicate things I'll separate all the values with a comma and
space. *

Dim ListString As String
String = ""
Dim SelectedRecord As Variant
For Each SelectedRecord In [List Box].ItemsSelected
String = String + [List Box].Column(2, SelectedRecord) & ", "
Next SelectedRecord

You could then assign the value of ListString to a text box called [Text Box].

[Text Box] = ListString

There are a few properties which make list boxes extremely useful once you
get the hang of them. These are ItemsSelected, Selected, Column and ItemData.
Check 'em out!

Cheers

David

* Removing the final ", " from the end of ListString is a story for another
time. Suffice to say there are two relevant functions, StrLen, which gives
you the number of characters in a string, and Left, which returns the
left-hand part of a string of a specified number of characters in length.
 
Back
Top