C001 Type Primary Keys

GreenshootProgrammer

Registered User.
Local time
Today, 00:08
Joined
Jan 16, 2013
Messages
74
C001 Custom Autonumber

Is there a way I can format AutoNumber for a primary key, for example, have the first ID start as C001 then for the next entry Access will automatically go to C002, and so on and so forth?
 
Last edited:
RuralGuy,
the link dos not work.

Dale

EDIT:
Link works now. ?????
 
Last edited:
Hmm...works for me. Anyway, here's the quote:
How can I generate a customized AutoNumber?

There a couple of answers to this common question:

The quick and dirty answer
Append something to a regular AutoNumber field.

Example: Let's say you want an AutoNumber like:
2003-0001
2003-0002
2003-0003
...etc...

You can't do this at the table level, as you can't make one field refer to another. However, you could create a field (AutoDateField) with a default value of Year(Date()) and always display the fields together:

AutoDateField & "-" & AutoNumberField

You can extend this concept of appending something to an AutoNumber field as appropriate for your needs.

That being said, let's move on to...

The proper answer
You don't want an AutoNumber. The main problems with using an AutoNumber field as a user-visible field: •You are not going to be able to guarantee the number remains sequential.
•You can not change the number.
•You can not "re-start" or skip numbers (in the above example, you're going to have problem come 2004. Think about it.)

The point is an AutoNumber is intended to be an AutoNumber, not a SequentialNumber. It's intended to provide a system-visible unique record identifier, not a user-visible one. It's so close it really makes you want to use an AutoNumber, but it's not close enough.

The proper way, then, is to create your own function that creates the number sequence you need.
Have the last1 number stored in a table. It would probably be a good idea to make this a one record table, with this value the only value in the record. A one-value table.

When a new sequence number is needed, retrieve the number. Lock the table so no other user can pull the same number until the current number is used. This will prevent duplicate numbers and breaking the sequence.

After the record is committed, you can store the new number and unlock the table.

There are some possible variations on this scheme. For example, you don't absolutely have to store the number in a table. You could search the target table for the most recent number and work off that. You do run into the possible problem of two users trying to use the same number at the same time. The method commonly used for this variation is the DMax() function. Given our earlier example of a sequential number by year, that DMax() might look like:
Nz(DMax("NumberField", "TableName", "DateField = " & Year(Date())), 0) + 1

The DMax() looks in the table for the largest value in the field named "NumberField" where the year in "DateField" is equal to the current year, and adds one to it. The Nz() function ensures that the first record of the new year works correctly. You can use other fields instead of year, to meet your needs. For example, some need numbers by department or company.

Either approach has the potential to deny a user a number, though. It's a matter of where you feel it is better to deny the number, before the record editing begins, or during record editing.
 
Or, in the field properties in design view enter A000 into the 'Format' property and select increment in the 'New Values@ property. :D
 
Dont' use Format to change the way the autonumber looks. It won't work the way you expect it to.

Use the code suggested by pbaldy to generate your own id if you want customization.
 
I couldn't figure out how to adapt it to the format I wanted it in and when it tried the explained method it wouldn't update the records. If someone could find the same tutorial but for a A001 type format I'd be greatful.
 
I guess you didn't read post #10.

If you want a formatted number, generate your own. An autonumber is intended to be a unique identifier. It isn't guarenteed to be continuous without gaps and it cannot include any user defined characters. There are lots of posts here with suggestions on how to generate your own sequence number using DMax() and you can concatenate the sequence with a text string and formate it as you like. In general, I would leave the autonumber as the PK and us it for all relationships and generate the custom number as a "visible" identifier given to the users and printed on documents.

PS - if you go with the autonumber PK, you will need to add a unique index for your prefix + sequence fields to ensure duplicates are not created.
 
Re: A001 Custom Autonumber

Is there a way I can format AutoNumber for a primary key, for example, have the first ID start as C001 then for the next entry Access will automatically go to A002, and so on and so forth?
I'll have to admit, "C001 to A002" does not make any sense to me. What is the logic?
 
There is no compelling reason these should be the PrimaryKeys of this table and several reasons for them not to be. I would use an AutoNumber for the PK of the table and another field for your values using the DLookup scheme already described. The client should never see this PK but you can easily find any record with your other "number". How do you determine Staff, Course or Trainee records?
 
At the minute I am using the following input masks and default values:
"S"000 S000
"C"000 C000
"T"000 T000
(field size: 4)

When searching queries I enter their ID.
 
How would a computer determine Staff, Course or Trainee records without the help of a human?
 
I'm not sure I understand your question but I think the answer is their ID PK. (they're separate tables)
 
Are we talking about three different tables? I thought it was three different types of records in one table.
 

Users who are viewing this thread

Back
Top Bottom