How to set count in form with a maximum that can't be exceeded...

B

bbarkman

I have a sub form that calculates the number of students enrolled in a class.
The room that the class meets in has a capacity. When I enter one too many
students, I would like my form to stop me so that I don't overload a
classroom. The calculation at the moment is located in the footer of the sub
form and works but it only counts the number of students on the list. It
does nothing else. Thanks in advance for any suggestions.

bbarkman
 
6

'69 Camaro

Hi.
I would like my form to stop me so that I don't overload a
classroom.

You're in luck. Someone asked a similar question yesterday. One method of
preventing the user from entering too many records is to limit the number of
new records that can be entered on the subform. Set the subform's Cycle
Property to "All Records." Use the following code in the subform's
OnCurrent( ) event:

' * * * * Start Code * * * *

Private Sub Form_Current()

On Error GoTo ErrHandler

Dim recSet As DAO.Recordset
Dim fOpenedRecSet As Boolean

Set recSet = Me.RecordsetClone
fOpenedRecSet = True

If (Me.NewRecord And _
(recSet.RecordCount >= Nz(Me.Parent!txtCapacity.Value, 0))) Then
recSet.MoveLast
Me.Bookmark = recSet.Bookmark
Else
Me!txtStuff.SetFocus ' Set focus to first editable control.
End If

CleanUp:

If (fOpenedRecSet) Then
recSet.Close
fOpenedRecSet = False
End If

Set recSet = Nothing

Exit Sub

ErrHandler:

MsgBox "Error in Form_Current( ) in " & vbCrLf & Me.Name & _
" form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

' * * * * End Code * * * *

.... where txtStuff is a text box in the subform that may be edited by the
user, and txtCapacity is a text box on the main form that holds a number
indicating the maximum capacity.

A new record will not be created, so if the primary key is an AutoNumber, it
won't increment when a new record is attempted and aborted. Therefore, it
has the effect of cycling up to the maximum number of records allowed, so
that tabbing after the last control will return focus to the first control on
that last record (which is then moved to focus on the first editable control
in the example above).

This method doesn't warn the user that the maximum number of records have
already been entered into the subform, but when the Cycle Property is set to
"Current Record," the user is never warned that only one record may be
entered, either. And since you have the calculation located in the footer of
the subform to count the number of students, you may want to put an alert
there (red font, perhaps?) that the maximum capacity has been reached for
this class.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)

- - -
When you see correct answers to your question posted in Microsoft's Online
Community, please sign in to the Community and mark these posts as "Answers,"
so that all may benefit by filtering on "Answered questions" and quickly
finding the right answers to similar questions. Remember that questions
answered the quickest are often from those who have a history of rewarding
the contributors who have taken the time to answer questions correctly.
 
B

bbarkman

Thanks! I will implement this right away. I don't spend much time dealing
with code or event procedures though. Actually I try to avoid it if
possible. But if it works, I guess it's time to learn a little bit about
code.

Thanks again!
 
B

bbarkman

Ok, this appears to be working, but I am getting error messages the moment I
open a class. Could it be because there are no students listed yet? It
doesn't bother me since I can still use the form after I close those error
boxes.

I like how it prevents me from adding too many records. Now, I have a
second form that on which the student's name is shown and the subform allows
me to select the classes to enroll them in (it's the reverse of the other
form). Can this form contain the same parameter so that if I choose a class
that is already at capacity this form will stop me?

Also, I'm a little confused about how to cause a msgbox so that if I try to
add one too many students I'll know it. I used a macro to set up a message,
but can't get it to appear when I enter the students.

BTW, thanks again for the first bit of code. It worked the moment I put it
in.
 
6

'69 Camaro

Hi.
Could it be because there are no students listed yet?

Correct. The original code expects at least one record in the table. The
following example code should work for you whether or not there are any
records in your table. It will also give the user a warning message when
the class is full:

' * * * * Start Code * * * *

Private Sub Form_Current()

On Error GoTo ErrHandler

Dim recSet As DAO.Recordset
Dim fOpenedRecSet As Boolean

Set recSet = Me.RecordsetClone
fOpenedRecSet = True

If (Not (recSet.BOF And recSet.EOF)) Then
If (Me.NewRecord And _
(recSet.RecordCount >= Nz(Me.Parent!txtCapacity.Value, 0))) Then
recSet.MoveLast
Me.Bookmark = recSet.Bookmark
MsgBox "No more students may be added" & vbCrLf & _
"to this class.", vbInformation + vbOKOnly, "Class Full"
Else
Me!txtStuff.SetFocus ' Set focus to first editable
control.
End If
End If

CleanUp:

If (fOpenedRecSet) Then
recSet.Close
fOpenedRecSet = False
End If

Set recSet = Nothing

Exit Sub

ErrHandler:

MsgBox "Error in Form_Current( ) in " & vbCrLf & Me.Name & _
" form." & vbCrLf & vbCrLf & _
"Error #" & Err.Number & vbCrLf & Err.Description
Err.Clear
GoTo CleanUp

End Sub

' * * * * End Code * * * *
Now, I have a
second form ...
Can this form contain the same parameter so that if I choose a class
that is already at capacity this form will stop me?

If and only if the txtCapacity text box on the main form is filled with the
maximum capacity of the class that is _going to be_ selected by the user.
If your code can anticipate this choice before focus goes to the subform,
then yes, the example code will work in this subform as well.

Good luck.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address, so that a message
will be forwarded to me.)
Beware to those who use munged addresses: known newsgroup E-mail harvesters
for spammers are (e-mail address removed) and (e-mail address removed)
 

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