Multi-Select List Box

K

Kieran Hanley

Any help on this would be great.
I need to put a multi-select list box on a form. Max of 4
items in the box.
How do I save the selections to different fields in the
table. Such that Selection 1 is in Field1, Selection2 in
Filed2 Etc.
 
D

Douglas J. Steele

In the AfterUpdate event of the ListBox, you'll need to put VBA code to
determine which rows were selected, and assign them to the appropriate
fields.

Something along the lines of the following untested air-code:

Dim intLoop As Integer
Dim strField As String
Dim varItem As Variant

intLoop = 1
For Each varItem In Me.ListBoxName.ItemsSelected
If intLoop > 4 Then
Exit For
Else
strField = "Field" & intLoop
Me.Controls(strField) = Me.ListBoxName.ItemData(varItem)
intLoop = intLoop + 1
End If
Next varItem


Of course, the fact that you've got fields like Field1, Field2, ... implies
that your database hasn't been properly normalized. You may find it better
to store the selected values in a second "child" table of your main table.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top