sean
sean

Reputation: 9268

Custom primary key for MS Access

I am new in microsoft access. I was just wondering on how can I use create a custom primary key? for example abc-123 format?

Upvotes: 0

Views: 8113

Answers (2)

Albert D. Kallal
Albert D. Kallal

Reputation: 49049

Actually, you could create a table trigger if using 2010 or later. The table trigger could take some field (where you get the abc from) and then some other field (seq num) and then add + 1 to the value.

The "air" code would look like this:

enter image description here

The beauty of the table trigger is it runs at table (data engine) level, and thus if you open the database with ODBC, VB.net, FoxPro, Access etc. then the PK key will always auto generate for you.

Upvotes: 2

Christian Specht
Christian Specht

Reputation: 36431

It depends on how you want the abc-123 values to be created.

If you want to create them by yourself in your code, just create a Text column and use that as your primary key.

If you want Access to create these values...that's not really possible. The only thing that Access is able to auto-generate are increasing numerical values (data type AutoNumber).

So the best thing you can do is to use an AutoNumber internally as the actual primary key, and create the abc-123 value out of that, just for displaying.

Here are some examples how to do this, from previous similar questions that I answered in the past:


Disclaimer: I don't know if a similar approach would work in your case.
If not, you need to give more information how exactly you want your numbers to be created:

  • do you want the number to increase?
  • do you want the letters to change/"increase"/always stay the same?

Upvotes: 2

Related Questions