You are here:

Using MS Access/Sequential numbering with a prefix

Advertisement


Question
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.SubProject.SetFocus
   Else
       Me.No = Nz(DMax("No", "tActionItems", "SubProject=" & Me.SubProject & ""), 0) + 1
       [ItemNo] = [SubProject] & Format([No], "0000")
   End If
   
   DoCmd.RunCommand acCmdSaveRecord
   DoCmd.RunCommand acCmdRefresh

End Sub

Answer
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,
Scott<>
Blog: http://scottgem.wordpress.com
Website: http://www.diamondassoc.com
Microsoft Access MVP 2007
Author: Microsoft Office Access 2007 VBA

Using MS Access

All Answers


Answers by Expert:


Ask Experts

Volunteer


Scottgem

Expertise

I can answer almost all types of questions relating to Microsoft Access usage and application design. My strengths are database and interface design.

Experience

I've been designing databases for over 25 years working with dBase, FoxPro, Approach and Access.

Organizations
Author of Microsoft Office Access 2007 VBA
Techncial Editor for Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports & Queries From Que Publishing

Education/Credentials
Brooklyn College BA

©2016 About.com. All rights reserved.