Controlling numbers in Multiselect Listboxes..

F

Fred Boer

Hello!

I have two multiselect listboxes on a form for a field day application. A
command button will move selections from a list of possible field day events
to a list of "selected" field day events. A student cannot choose more than
7 events. I want to prevent the addtion of more than 7 items in the
"Selected Events" listbox. I have a textbox which displays the number of
events selected. This texbox is based on query, mostly because I couldn't
work out how to do the count of items in code. I *thought* I had this
working, but when I select more than 1 event at a time, and click the
button, no error message pops up.

I would appreciate someone explaining why this doesn't work with multiple
selections! Also, if there is a better way (and I feel certain there must
be...) could someone point me in the right direction... Code below...

Thanks!
Fred Boer


Private Sub cmdAdd_Click()
Dim varItem As Variant
Dim rst As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb()
Set rst = db.OpenRecordset("Select * from tblStudentEvent where
Student_ID=-1")
If Me.lstEvents.ItemsSelected.Count = 0 Then
MsgBox "Before clicking this button you must first make a selection.",
vbExclamation + vbOKOnly, "Field Day"
Else
For Each varItem In Me.lstEvents.ItemsSelected
With rst
If Me.txtNumberSelected > 6 Then
MsgBox "You cannot enter more than seven events!", vbExclamation +
vbOKOnly, "Field Day"
Else
.AddNew
.Fields("Student_ID") = Me.Parent.txtStudent_ID
.Fields("Event_ID") = Me.lstEvents.ItemData(varItem)
.Update
End If
End With
Next varItem
End If

rst.Close
Set rst = Nothing
Set db = Nothing

Me.lstEvents.Requery
Me.lstChosenEvents.Requery

End Sub
 
J

Jonathan Parminter

Hi Fred,
consider using the listbox click event to test number of
items selected...

Private Sub List0_Click()
MsgBox "Selected=" & List0.ItemsSelected.Count
End Sub

you can then perhaps disable the addition button to denigh
further selections. Re-enable when user has removed a
selected item...

Luck
Jonathan
 
F

Fred Boer

Thanks for responding! I am sure I am being dense, but I am having a little
trouble visualizing your suggestion. Suppose there are 4 events already
selected, and the user multiselects 5 more. The way it is now, they somehow
avoid my trap and slide over into the "Selected" listbox as a group... no
msgbox. So you are suggesting that when the user clicks into the unselected
list box a messagebox pops up listing how many items are already selected? I
don't see how that will work - could you give me a little more detail on how
this might work... I'm obviously missing something.. sorry! :(

Fred
 

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