MS Access VBA – Automatic Creation of Unique Alphanumeric ID

ms accessms-access-2010vba

All the accounts in my database have a unique ID, that begins with the letter "G" (always), and has a 3 digit number following (e.g. "G026", "G123","G005","G999", etc.)

When a user is inputting new-account information through a form, I would like the unique ID to be auto-generated. So I would like a unique "G000" formatted number that is NOT already taken up in the list of users. I have tried a few things, but am having a lot of trouble getting it to work.

Does anybody have any ideas of a module I could implement? I'm thinking of making a module that generates these numbers, and calling this module in the "Default Value" field.

THANK YOU!!

Best Answer

This will work. There may be something more elegant but this should get you started.

 Public Function GetUID() As String
      GetUID = "G" & Right("000" + CStr((CInt(Right(DMax("[YourIDField]", "TableContainingField"), 3)) + 1)), 3)
 End Function