OK, as long as you are controlling the input of new data via Forms, this can be done in the following manner.
I am making the following assumptions, which are built into my solution. So you will need to make changes to the names where necessary.
Name of data table:
Table1
Name of Serial Number field:
SerialNum
First, go into the data table, and change the Properties of the SerialNum field so that the following Properties are set:
Data Type: Number
Required: Yes
Indexed: Yes (No Duplicates)
Now, create a new query that returns all records from your data table that have a SerialNum value with today's date in it. Do that by:
- Create a new query based on Table1
- Double-click the SerialNum field in order to return it in the query grid
- On the Criteria row of the query grid under the SerialNum field, add this formula:
Code:
Like Format(Date(),"yy") & Format(Date()-DateSerial(Year(Date())-1,12,31),"000") & "*"
- Save the query as
qryMaxSerialNumber
OK. Now go to your entry Form. Make sure that you add the SerialNum field to the Form, and it is bound to the underlying field in Table1.
Go to the Properties of this SerialNum text field and set the Enabled property to "No". This will prevent users from editing it.
Add a Command Button to your Form. This will be used to populate the SerialNum field so that the record can be added (remember, we made it a required field).
Go to the Properties of the Command Button, go to the Event tab, and choose the [Event Procedure] option in the "On Click" row, and enter the following VBA code between "Private Sub ..._Click()" and "End Sub":
Code:
Dim RecCount As Long
Dim JulianDate As String
Dim NextSerialNum As Long
' If SerialNum is already populated, exit
If Me.SerialNum.Value > 0 Then
MsgBox "Record aleady has a SerialNum value assigned to it"
Exit Sub
End If
' Count number of records added with today's date
RecCount = DCount("SerialNum", "qryMaxSerialNumber")
' Calculate next record number
If RecCount = 0 Then
JulianDate = Format(Date - DateSerial(Year(Date) - 1, 12, 31), "000")
NextSerialNum = Format(Date, "yy") & JulianDate & "01"
Else
NextSerialNum = DMax("SerialNum", "qryMaxSerialNumber") + 1
End If
' Populate SerialNum
Me.SerialNum.Value = NextSerialNum
This code should do what you want.