Others

Create Year-Based Serial Numbers in Microsoft Access 2016

John WilsonJohn Wilson Nov 18, 2026 869 views

Question details

Create a unique serial number for each new Access record, with a sequence that resets each year.

Create Year-Based Serial Numbers in Microsoft Access 2016
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.
Before you start

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.

Solution 1Recommended

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.

1
Set up the serial number field

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.

2
Bind the form control

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.

3
Add the form event

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

4
Test a new record

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.

Generate serial numbers in an Access form with VBA
Concurrent entry and year limits: The serial number appears when the new record is saved, not when entry begins. If two users save at nearly the same time, they can select the same next number; the unique index rejects the duplicate. Retry the failed save so the form can calculate the next available number. The example supports up to 9999 records per year and uses the computer's current date.
Free Microsoft Office alternative

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.

Review and organize serial-number lists in a spreadsheetOpen and save Microsoft Excel XLSX filesUse a familiar spreadsheet interfaceWork with a free, lightweight office suite
microsoft office alternative - wps office

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.