Others

Automatically Number Lessons by Project in Microsoft Access

Jason MillerJason Miller Nov 25, 2026 868 views

Question details

Assign a separate sequential lesson number within each project in an Access database.

Automatically Number Lessons by Project in Microsoft Access
Product
Microsoft Access
Device & OS
not provided
Scenario
A user selects a project while entering a new lesson record.
Observed behavior
Each new lesson should receive a number one higher than the highest saved lesson number for its project.
Before you start

Confirm that the Lessons table has separate ProjectID and numeric LessonNumber fields and that your data-entry form is bound to the table. The example below assumes ProjectID is numeric and that the form controls are named ProjectID and LessonNumber.

Solution 1Recommended

Calculate the next lesson number on the Access form

Use DMax when a project is selected, then add a unique index to prevent duplicate lesson numbers within that project.

DMax looks up the highest saved LessonNumber for the selected project. Nz returns zero when no lesson exists, so the first number is 1.

This method uses the current maximum, so deleting the highest-numbered lesson can allow that number to be reused. In a multiuser database, two users can calculate the same next number before either record is saved.

1
Open the project control event

Open the data-entry form in Design View, select the ProjectID control, and open its Property Sheet. On the Event tab, set After Update to [Event Procedure] and click the build button.

2
Add the numbering code

In the VBA editor, add this code inside the ProjectID_AfterUpdate procedure: If Me.NewRecord And Not IsNull(Me!ProjectID) Then Me!LessonNumber = Nz(DMax("LessonNumber", "Lessons", "ProjectID=" & Me!ProjectID), 0) + 1 End If

3
Create a composite unique index

Open the Lessons table in Design View and choose Indexes. Enter an index name and ProjectID on the first row, then leave the index name blank on the next row and select LessonNumber. Select the first row, set Unique to Yes in Index Properties, and save the table.

4
Test a new lesson

Open the form in Form View, create a new record, and select a project. Verify that LessonNumber is one higher than that project's current maximum, then save the record.

Calculate the next lesson number on the Access form
Handle duplicate save conflicts: The unique index prevents duplicate project and lesson number pairs but does not resolve simultaneous numbering attempts. If a save is rejected, recalculate the number from the latest saved records and try again. Existing duplicate pairs must be corrected before the unique index can be saved.
Free Microsoft Office alternative

Work with lesson lists in WPS Office

WPS Office is a free, lightweight alternative for working with lesson lists and other Office documents. Its familiar interface and Microsoft Office format compatibility make it easy to move existing files, although it does not replace an Access database or run Access form code.

Organize exported lesson and project lists in WPS SpreadsheetsOpen and edit Microsoft Excel XLSX filesUse a familiar interface for common document and spreadsheet tasksMove existing Office documents into a lightweight, free suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does the first lesson in a project get number 1?

DMax returns Null when it finds no saved lesson for the project. Nz converts that result to zero, and the expression adds 1.

Can two projects have the same lesson number?

Yes. The composite unique index on ProjectID and LessonNumber prevents duplicate pairs, while allowing different projects to each have a lesson numbered 1.

What happens if two users add lessons to the same project at once?

Both users may calculate the same number before saving. The unique index rejects the duplicate; recalculate the number from the latest saved records and retry the save.

Will this code work if ProjectID is a text field?

The example assumes a numeric ProjectID. A text criterion needs quotes around the value and must escape any embedded apostrophes before it is passed to DMax.