Autonumber with a prefix and starting at a specific number

Copper Contributor

Hi, I've used Access for a number of years, but I'm stumped now.   Making a database and need a primary key autonumber that has the prefix of CT and starts counting at 21000.    Can I accomplish this in the `format' field?   if not, what's the easiest way to pull this off?

In other words, first  record is CT21000, second is CT21001, CT21002, etc.

Thanks!

1 Reply

@craigmmm 

 

First off, AutoNumbers are a unique type of Long Integer. These are Numbers and do NOT include alpha characters. Therefore, to do what you plan, you will need to adapt one of two strategies, either of which could work. Choose the one you feel most comfortable with.

 

  • Continue to use the AutoNumber as you primary key. When you DISPLAY the AutoNumber (if you really want to do that) concatenate your prefix to it for DISPLAY ONLY. "CT" & [YourPrimaryKeyFieldGoesHere] 
  • Use a Text field instead. If you do that, you can store the prefix in the field along with the manually generated values you need.

There are pros and cons to both approaches, as you might expect.

 

AutoNumbers are designed to be unique and are best suited for INTERNAL USE ONLY in providing Primary Keys for tables in an Access relational database application. They cannot be guranteed to generate sequential values with no gaps, so you should not rely on them if sequential values without gaps is a requirement for your relational database application. However, they are by far the most reliable way to manage surrogate Primary Keys in tables, so they are usually the preferred option for that purpose.

 

If you decide you want to DISPLAY a sequential value and want to invest time in creating a VBA module to produce them, that's a different kind of task. Many people do use this approach for DISPLAYING sequential values to users for things like printing invoices, for example.

 

There are a number of examples of procedures to produce those sequential values on the internet. You'll find one of the best here.