Others

How to Automatically Number Access Subform Records for Each Patient

Khadija KhanKhadija Khan Oct 19, 2026 869 views

Question details

The user needs a method to generate sequential numbers for records in a Microsoft Access subform that restarts at 1 for each new patient, bypassing the limitations of AutoNumber.

How to Automatically Number Access Subform Records for Each Patient
Product
Microsoft Access
Device & OS
not provided
Scenario
Creating patient-specific subform records where each patient's entries must begin at 1 and increment sequentially without skipping numbers.
Observed behavior
Using the built-in AutoNumber field fails because it generates globally unique numbers that do not restart per patient and leaves permanent sequence gaps when records are deleted.
Before you start

Ensure you have a basic understanding of Microsoft Access Form design and VBA (Visual Basic for Applications), as you will need to add a custom event procedure to the subform.

Solution 1Recommended

Calculate the Next Serial Number Using the Subform's Current Event

Use a custom VBA script to calculate the highest existing serial number for the current patient and dynamically set the default value for the next record.

AutoNumber fields are designed solely to guarantee uniqueness across an entire table. They cannot restart at 1 for grouped records (like individual patients) and cannot prevent sequence gaps if an existing record is deleted.

To achieve seamless sequential numbering per patient, you must use VBA code within the subform's On Current event to evaluate the existing records and calculate the next available number dynamically.

1
Open Subform in Design View

Open your Microsoft Access database, locate the subform used for patient records in the Navigation Pane, right-click it, and select 'Design View'.

2
Access the Form Properties

Press the F4 key on your keyboard to open the Property Sheet. Ensure the Selection type at the top of the Property Sheet is set to 'Form'.

3
Create an Event Procedure

Navigate to the 'Event' tab in the Property Sheet, locate the 'On Current' property, click the drop-down arrow to select '[Event Procedure]', and then click the ellipsis (...) button to open the VBA Editor.

4
Add the DMax Calculation Code

In the VBA Editor, insert a DMax function to find the highest existing number for the current Patient ID. Use this syntax: Me.YourSequenceField.DefaultValue = Nz(DMax("YourSequenceField", "YourTableName", "PatientID = " & Me.PatientID), 0) + 1. Update the field and table names to match your actual database schema.

5
Save and Test the Form

Save the VBA code, close the editor, and switch your main form to 'Form View'. Add a new record for an existing patient to verify the sequence number starts at 1 or increments correctly from the previous entry.

Calculate the Next Serial Number Using the Subform's Current Event
Multi-User Environments: If your Access database is shared among multiple users, consider adding validation rules or record-locking logic to prevent two users from generating the same sequence number at the exact same time.
Free Microsoft Office alternative

Manage Your Data Seamlessly with WPS Office

While Microsoft Access is built for complex relational databases, managing lists and tabular sequences is often much easier in a spreadsheet environment. WPS Spreadsheet offers an excellent, lightweight alternative for organizing and analyzing data without the need for complex VBA programming. Experience a full-featured office suite that is completely free and highly compatible with Microsoft formats.

  1. 1. Download and Install WPS Office: Visit the official WPS website to download and install the free office suite tailored for your operating system.
  2. 2. Import Your Data: Easily open your exported CSV or Excel files from Microsoft Access directly into WPS Spreadsheet.
  3. 3. Use Formulas for Sequences: Leverage standard spreadsheet formulas to automatically sequence and categorize your patient records without writing a single line of VBA code.
Fully compatible with Microsoft Excel (.xlsx, .xls) and other Office document formats.Built-in advanced formulas like COUNTIF and ROW for effortless sequence numbering per category.Lightweight software that runs smoothly on Windows, Mac, Linux, iOS, and Android.Familiar, intuitive user interface for a seamless migration from Microsoft Office.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my AutoNumber field skip numbers in Microsoft Access?

AutoNumber fields are designed exclusively to generate unique identifiers, not continuous sequences. If a record is deleted or an insertion is canceled midway, the generated AutoNumber is permanently discarded, resulting in visible gaps in your sequence.

Can I conditionally reset the AutoNumber field to 1 for a new patient?

No, an AutoNumber field cannot be conditionally reset to 1 based on another field's value (such as a Patient ID). It increments continuously for the entire table. You must use a custom VBA numbering method for patient-specific sequences.

What is the DMax function used for in Access VBA?

The DMax function is a domain aggregate function used to determine the maximum value in a specified set of records. In sequence numbering, it is utilized to find the highest existing serial number for a specific patient so the script can accurately add 1 to generate the next number.