Multiple Selections (And Saving As Text Delimited) For List Box

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
I have a list box called LbxNAC and I want the user to be able to select multiple choices.
I know, I know. Not good practice, should use a subform/combo box one to many...blah blah
Point is, the users are not too bright and a straight forward list box would be best
considering I have about 15 options for different selections.

Could someone please provide a working code for the listbox...My table is called Prospects table
My list box is called LbxNac...just need a code (possibly using those names)

I am humbled by any really you may give
(And I don't think I'll answer my own question this time, like past messages..lol)
Many Thanks!!
 
Blas,

Supply a working code to do what? You haven't explained
what the result of the multiple selections will be supposed
to do.

Gary Miller
Sisters, OR

message
Hello,
I have a list box called LbxNAC and I want the user to
be able to select multiple choices.
I know, I know. Not good practice, should use a
subform/combo box one to many...blah blah
Point is, the users are not too bright and a straight forward list box would be best
considering I have about 15 options for different selections.

Could someone please provide a working code for the
listbox...My table is called Prospects table
 
OK, it's tricky to make this work (hence, the reason it's considered bad
practice).

The first thing you need to do is create a hidden textbox on your form.
This will hold the actual text delimited value, and will be bound to the
table

The second thing you need to do is create the code to extract the values of
the listbox. If you look at the code in the help file for MultiSelect
listboxes, it's surprisingly helpful. The code should look vaguely like:

Dim varItm as Variant
Dim strNewVal as String

strNew = ""

For Each varItm in Me.LbxNAC.ItemsSelected
strNew = strNew & Me.LbxNAC.ItemData(varItm) & "," 'Or whatever
delimiter you want
Next varItm

Me.txtNAC = strNew


Now comes the tricky part. Where to put this code. If you put it in the
AfterUpdate event of the listbox, you'll be sure to catch all the changes.
But, it will run every time someone clicks on any option in the box.
Depending on your set-up, this may be horrifically slow. Putting the code
in any other event runs the risk of changes to the options not being
captured properly.

And, now, the REALLY tricky part. Assuming you want the users to see the
list box with its selections when they look at the record, you're going to
need to write some hefty code. You'll need to parse the contents of the
text box. You will then need to match those contents up to the rows in the
listbox, so that you can mark the proper rows as selected. I'm not sure off
the top of my head how to pull that off. Oh, yeah, and you need to be
careful that you do not kick off the AfterUpdate event in the middle of
setting the selections, or you will create a loop that could do "bad things
(TM)."

All in all, I think you're much better off just telling your users to deal
with a subform. Or, if your list of options is fairly small and static, you
could even create a series of checkboxes, which would be much easier to
handle with code.
 
Back
Top