Create a table named tblSeqNum with on Long field named SeqNum and a Date
field called RestartDate.
The default value for the serial number field on your form will be:
=DMax("SeqNum","tblSeqNum")
Also the format for your serial number needs to be 000 in the table, forms,
and all reports to show the leading zeros.
In the BeforeInsert event of your form you need the following code:
Private Sub Form_BeforeInsert(Cancel As Integer)
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE tblSeqNum SET tblSeqNum.SeqNum =
[tblSeqNum]![SeqNum]+1;"
DoCmd.SetWarnings True
End Sub
OK. This will increment the number. It's possible that if
two people are entering records at the same time that the numbers could get
out of proper sequence. It also does not take into account if someone
creates a new record then deletes it.
Now what to do about starting over at the beginning of the month..
Put the code below in the Form Open event of the input form.
It checks the RestartDate whenever the form is open and updates it if over
a month old. Watch out for word wrapping if you cut and paste.
Private Sub Form_Open(Cancel As Integer)
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE tblSeqNum SET tblSeqNum.SeqNum = 1,
tblSeqNum.RestartDate = Now() WHERE Year([RestartDate]) &
Month([RestartDate])<Year(Date()) & Month(Date());"
DoCmd.SetWarnings True
End Sub
--
Jerry Whittle
Light. Strong. Cheap. Pick two. Keith Bontrager - Bicycle Builder.
MikeJohnB said:
Sorry if this is similar to any previous questions.
I have a table which contains.
Year 05
Month 12
Initials TMM
which when joined together generates
05/12/TMM, this part is simple
However, I want to add a serial number to the end
05/12/TMM/###
the serial number should start at 001 and increment by 1 until the end of
the month at which point it should restart at 001. Can anyone assist with
this?