Automatically Number Lessons by Project in Microsoft Access
Question details
Assign a separate sequential lesson number within each project in an Access database.

- 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.
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.
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.
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.
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
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.
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.

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.

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.




