This can get a little tricky, timing wise, if multiple people are creating records. So if you are running where multiple people are pulling a Key, you will want to pull the key and save the record right away, or wait to create the key on save.
Either way it's basically the same. Some people like to use a Key Table but most of the time it's easiest to just look up the next value with a function. Here is one:
- Public Function getKey(ByRef sTable As String, ByRef sField As String, ByRef iKeyLen As Integer, ByRef sPrefix As String) As String
-
Dim sLastKey As String
-
sLastKey = DMax(sField, sTable)
-
getKey = sPrefix & Right("00000" & Val((Right(sLastKey, Len(sLastKey) - Len(sPrefix))) + 1), iKeyLen - Len(sPrefix))
-
End Function
-
You can use this type of function as the DefaultValue for a Control on a Form or have it called from VBA if you are doing some magic to make sure all your many users are getting unique values.