My Assistant
![]() ![]() |
|
|
Mar 15 2007, 01:06 PM
Post
#1
|
|
|
UtterAccess Member Posts: 43 From: Manchester UK |
I want Access 2007 to create a Primary Key based on the first 4 letters of the Company name and add a number to the end increasing by 1 for additional instances of the same Company.
Example for Company Table The first record for a Company called Edward & Sons would have a primary key of EDWA01 and the second record for the same company would be EDWA02. I want Access to generate these PKs automatically when a new record is saved and be able to establish if there is already a Company of the same name in the database and add the numbers at the end all by itself. (for branches of the same Company) Example for Contacts Table (related to the Company table as the many side of the 1 to many relationship) I want Access 2007 to use the system generated Company Name PK & add a C to it (to stand for contact) and then increase the last digit by 1 each time a new contact is created for that Company. Again I think this is more meaningful than an auto number. Example EDWA01C1 would be contact 1 for Edwards & Son Ltd. EDWA01C2 would be the 2nd contact for the same Company. The contacts will be displayed in a subform of the main form which is the Company form. I would appreciate help with this as I am about to create the Company table and start putting 1000+ company records in. I am also about to create the contacts table which has more records. Feedback on my way of doing it would be appreciated as this is my first Access database. Is what I am thinking of doing a good idea? nb As I delete records over time as and when required do the system generated PKs just keep going up and gaps will just appear in the sequence over time. |
|
|
|
Mar 15 2007, 01:27 PM
Post
#2
|
|
|
UtterAccess VIP Posts: 1,405 From: Heart of England |
Simply put, the PK should just be an autonumber and be of no relevance to the users of the database/application
To create custom "references" see the thread here HTH |
|
|
|
Mar 15 2007, 01:31 PM
Post
#3
|
|
|
Retired Moderator Posts: 10,493 From: Lexington/Louisville KY USA |
Natural v. surrogate keys is an often debated topic. This thread is but one example.
I would personally never use a PK like you describe. It's text, which will make the index slow, and it's a calculated value. What happens when a company changes it name. In Access, your best bet, in my and most other regular contributors here at this site, opinion, is an autonumber PK. You can have an alternate index on another field, but I would not store the first four characters of the name over again. You already have them. You can store a sequential number and concatenate your desired expression any time you want it. |
|
|
|
Mar 15 2007, 01:44 PM
Post
#4
|
|
|
UtterAccess VIP Posts: 5,635 From: Chicagoland, USA |
To add to the discussion, what happens when multiple companies start with the same four letters?
|
|
|
|
Mar 15 2007, 06:35 PM
Post
#5
|
|
|
UtterAccess Member Posts: 43 From: Manchester UK |
Thanks Sleepy Chicken. I have read other threads and am now convinced to use the autonumber.
|
|
|
|
Mar 15 2007, 06:38 PM
Post
#6
|
|
|
UtterAccess Member Posts: 43 From: Manchester UK |
Thanks Noah, I have read the thread and the big debate and am now convinced to use the autonumber as PK. (it's for Access not us humans lol )
|
|
|
|
Mar 16 2007, 08:23 AM
Post
#7
|
|
|
Retired Moderator Posts: 10,493 From: Lexington/Louisville KY USA |
It is, of course, your decision, but, from my own experience, I think you'll find yourself much better off this way.
|
|
|
|
Mar 20 2007, 04:20 AM
Post
#8
|
|
|
UtterAccess Member Posts: 43 From: Manchester UK |
Thanks Noa
|
|
|
|
![]() ![]() |
|
Go to Top · Lo-Fi Version | Time is now: 23rd May 2013 - 01:01 AM |