Others

How to Calculate a Year-Based Sequential Order Number in Microsoft Access

Algirdas JasaitisAlgirdas Jasaitis Oct 23, 2026 868 views

Question details

The user needs to generate an OrderNumber in an Access continuous form, formatted as a two-digit year followed by a four-digit sequence (e.g., YY-NNNN) that resets at the start of each year.

How to Calculate a Year-Based Sequential Order Number in Microsoft Access
Product
Microsoft Access
Device & OS
not provided
Scenario
Creating a sequence number generator in a multi-user database that avoids duplicate key conflicts during simultaneous data entry.
Observed behavior
Attempting to generate the sequence using default value expressions fails to reliably populate the field without requerying the form, which is problematic for multi-user environments.
Before you start

Ensure you have full design access to your Microsoft Access database and understand basic VBA logic, as creating a reliable multi-user sequence requires adding code to form events rather than relying on simple default values.

Solution 1Recommended

Use VBA on the Form's Before Insert Event

This is the most reliable method for multi-user databases. By generating the sequence number at the exact moment a record is saved, you minimize the risk of two users receiving the same number.

Storing the year and the sequence number in two separate table fields prevents complicated string extraction. You can later concatenate them for display purposes.

Using the Before Insert event ensures the number is generated right before the record is saved, reducing conflicts. To fully handle conflicts, you must also add duplicate-key error trapping.

1
Create separate fields in your table

Open your target table in Design View. Create two numeric fields: one named 'OrderYear' (Integer) and one named 'OrderSeq' (Long Integer).

2
Access the Before Insert event

Open your continuous form in Design View. Select the form itself, open the Property Sheet, navigate to the 'Event' tab, and click the ellipsis (...) next to 'Before Insert'.

3
Write the DMax VBA code

In the VBA editor, write code to find the highest sequence for the current year. Use: NextSeq = Nz(DMax("OrderSeq", "OrderT", "OrderYear = " & Year(Date)), 0) + 1.

4
Assign the values to the form controls

Assign the calculated values to your bound fields: Me!OrderYear = Year(Date) and Me!OrderSeq = NextSeq.

Use VBA on the Form's Before Insert Event
Display Format: To display the YY-NNNN format in forms or reports, you do not need to save the hyphenated string in the table. Simply use an unbound text box with the control source: =Right([OrderYear], 2) & "-" & Format([OrderSeq], "0000").
Free Microsoft Office alternative

Looking for a Free Office Suite? Try WPS Office

While Microsoft Access is a powerful tool for complex database management, WPS Office provides an excellent, free, and lightweight suite for all your document, spreadsheet, and presentation needs. It's fully compatible with Microsoft Office formats and helps streamline your daily office tasks without heavy subscription fees.

Free and lightweight Office suite alternativeHigh compatibility with Microsoft Word, Excel, and PowerPoint formatsFamiliar, easy-to-use tabbed interfaceSeamless migration and built-in PDF editing tools
microsoft office alternative - wps office

Frequently Asked Questions

Can I use an AutoNumber field to generate a YY-NNNN sequence?

No. AutoNumber fields in Access are designed solely to provide unique identifiers for database records. They cannot be formatted to reset every year, nor can they guarantee sequential order without occasional gaps.

Why does my sequence number occasionally skip values?

If a user begins entering a record (which calculates the next sequence) but cancels the entry or encounters a system error, that specific sequence number is often discarded. This is normal in database environments to prevent record deadlocks.

How do I handle duplicate key errors when two users save simultaneously?

In your VBA code, you must include an error handler that specifically traps Error 3022 (duplicate index). When this error is caught, the code should instruct Access to recalculate the DMax function for the next available number and automatically retry the save operation.