Using MS Access/Sequential numbering with a prefix
I'm trying to apply an automated sequential number to an already existing action item list that was imported from an excel sheet. the list has 26 existing records. The items are separated by sub-project and I want the item number to have a 3 letter prefix to indicate the sup-project like so, "XXX0001". So, I created fields for "No" as Number and "SubProject" as Text, then "ItemNo" as Text.
I trying to use a Dmax statement to increment the No field in a click event of my Save button on the entry form. I can't seem to get any thing to work. I've gotten the Dmax statement to work in tables where I didn't have assign any criteria but not here. Here is the procedure:
Private Sub cmdSave_Click()
If IsNull(Me.SubProject) Then
MsgBox "Sub Project must be selected before saving!"
Me.No = Nz(DMax("No", "tActionItems", "SubProject=" & Me.SubProject & ""), 0) + 1
[ItemNo] = [SubProject] & Format([No], "0000")
First, I address this in my blog article on Sequential Numbering.
Second, remove the ItemNo field from your database, its redundant and unnecessary. Just use expression:
[SubProject] & Format([No], "0000")
As the Controlsource of an unbound textbox or wherever you need to display this identifier.
Third, Since Subproject is a text filed your code needs to be:
Me.No = Nz(DMax("[No]", "tActionItems", "[SubProject] = '" & Me.SubProject & "'"), 0) + 1
You need the single quotes to surround the text value.
Hope this helps,
Microsoft Access MVP 2007
Author: Microsoft Office Access 2007 VBA