Create Year-Based Serial Numbers in Microsoft Access 2016
Question details
Create a unique serial number for each new Access record, with a sequence that resets each year.

- Product
- Microsoft Access 2016
- Device & OS
- not provided
- Scenario
- A user enters records through an Access form and needs identifiers such as 0001/19 and 0002/19.
- Observed behavior
- Each new record should receive the next sequence for the current year, starting again at 0001 when the year changes.
Identify the table and bound data-entry form that will store the serial number. The example uses a table named SerialNumbers and a Short Text field named SerialNo; replace these names in the code if yours differ.
Generate serial numbers in an Access form with VBA
Use the form's Before Update event to assign the next number when a new record is saved.
The code searches existing serial numbers for the current two-digit year, finds the highest four-digit sequence, and adds one. It assigns the value through the form rather than relying on a table default.
This example assumes existing values follow the 0000/yy pattern. A two-digit year repeats every century, so use a four-digit year in the design if records must remain distinguishable across centuries.
Open the SerialNumbers table in Design View. Create or select the SerialNo field, set its data type to Short Text and its Field Size to 7, then set Indexed to Yes (No Duplicates). Save the table. Resolve any existing duplicate values before applying the index.
Open the data-entry form in Design View. Set its Record Source to SerialNumbers, then add or select a text box and set its Control Source to SerialNo. Leave the control's Default Value empty.
Select the form, open the Property Sheet, and on the Event tab set Before Update to [Event Procedure]. Click the build button (...) and add this code to the form's VBA module: Private Sub Form_BeforeUpdate(Cancel As Integer) Dim yy As String Dim nextNo As Long If Me.NewRecord Then yy = Format(Date, "yy") nextNo = Nz(DMax("Val(Left([SerialNo],4))", _ "SerialNumbers", "Right([SerialNo],2)='" & yy & "'"), 0) + 1 If nextNo > 9999 Then MsgBox "The sequence has reached 9999 for this year." Cancel = True Exit Sub End If Me!SerialNo = Format(nextNo, "0000") & "/" & yy End If End Sub
Save the form, open it in Form View, enter a new record, and save it. Check that SerialNo is populated, then add another record to confirm that the sequence increments.

Track serial-number lists in WPS Spreadsheets
For spreadsheet-based lists, WPS Office is a free, lightweight alternative with a familiar interface and Microsoft Office format compatibility for an easier transition. It does not run Access forms or fix Access database automation.

Frequently Asked Questions
Why is the serial number assigned when I save the record?
The example runs in the form's Before Update event, so it calculates the number just before a new record is saved. Enter the record in Form View and save it to see the value.
How does the sequence reset for a new year?
The VBA code filters existing SerialNo values by the current two-digit year. If none match, DMax returns no value and the sequence starts at 0001.
How can I prevent duplicate serial numbers?
In the table's Design View, select SerialNo and set Indexed to Yes (No Duplicates). If simultaneous users receive a duplicate error, retry the failed save to calculate the next available number.
Can I enter records directly into the table and still get a number?
No. This example runs in the bound form's Before Update event, not in the table. Enter new records through that form for the VBA to assign the serial number.




