I am transitioning an Excel database I made, into Access. It includes a table of volunteers and a table of patients. In Excel I came up with unique IDs for each group (VID and PID) which I use as primary keys in Excel. In Access I have autonumber primary keys, but the VIDs and PIDs are very useful for helping staff look up records.
The IDs are 4-character strings. The first two characters are the person’s first and last initials, followed by a two-digit number, consecutively added as new people are entered. That’s what I need help setting up in Access.
Here’s what I’ve done so far. In both the First and Last name fields of the data entry forms, I have VBA code on the BeforeUpdate event that checks two things: Is the VID (or PID) field empty; Are both the First and Last names entered. If those conditions are true, I put the initials in a variable “Initials”.
What I want to do is look up the initials in the ID field of the corresponding table, determine the highest number assigned, then assign the next number to this new person.
I have a query that will return all the existing instances matching the initials and sort it in descending order. I have a condition to prompt for initials which is: Like [Initials] & “*”.
I can call the query from VBA, but only with static criteria. I don’t know how to pass the Initials variable to the query, and I don’t know how to collect the results or determine the next consecutive number.
These first steps may be going in the wrong direction. I welcome any help with this or better suggestions. This is one of several steps in trying to better understand how to develop Access. I have a lot of trouble with Access syntax and procedures, but bit by bit I’ll get it.
I hope someone here can help with this.
Thanks,
- Jeff