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
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