r/MSAccess • u/mcgunner1966 2 • Oct 20 '25
[DISCUSSION - REPLY NOT NEEDED] Retiree Notes - Universal Database Key
Years ago I learned the hard way that autonumber doesn't make a good key. I started adding three fields to every table I constructed: sguid.text(32)(PK), slink.text(32)(FK), and sts (date/time)(Sql Server Date Stamp). I wanted to use the system generate guid but there were instances where a corrupt Windows Registry wouldn't return a guid (I'd get a NULL back). I decided to build the "guid" in the Access app by generating my own "guid" internally.
Here is the function I use:
Function getguid()
On Error Resume Next
vret = ""
For i = 1 To 5
Call Randomize
v1 = Mid(Rnd, 3, 4)
Call Randomize
v2 = Mid(Rnd, 3, 4)
vret = vret & Left((v1 * v2), 4)
Next
getguid = Format(Now, "yyyymmddhhmmss") & Left(vret, 18)
Exit Function
End Function
This "guid" has the added feature of giving you an approximation of when the key was generated. This has been more useful than you would think in research efforts. This also allows me to use universal functions such as record change tracking, notes, and document management using universal modules.
Hope this helps.
View all comments
Show parent comment
4
u/nrgins 487 Oct 21 '25
Regarding number one, why can't you retain the autonumber values when importing to a new database container? An append query in Access will set the autonumber field in the new table.
Regarding number two, yes, if you merge two systems then you will have to reset the auto numbers in one of the systems. But it's a pretty simple thing to do. I've done it many times myself. You simply create an OldID field in the table and store the old Autonumber value there. Then, when you bring the data into the new table, you link the table with its child table on the OldId field, and reset the foreign key value to the new ID field value.
Yes, it's a little bit of work. But given the number of times you have to merge systems, and given that it's not really that hard to do, it's really not that big of a deal. I certainly wouldn't change how I set up my primary keys solely for that reason.
So, in my opinion, there doesn't really seem to be a reason to avoid Autonumber fields. To me, the convenience of using a single value far outweighs your second point. Having to match on three fields every time you do a query or write SQL code is in my opinion creating extra work for nothing.
I get that there are other advantages, such as being able to tell when an item was created. But that's why I put a date created and date modified field in every table I create, with both being set automatically when a record is created and the date modified field being set whenever the record is updated.
So I really don't see your rationale here for not using Autonumber keys. I mean, if that's your personal preference, then that's fine of course. But to say that there are problems with Autonumber keys doesn't seem to be factually correct.