I have faced a similar situation, and have come up with something like the
following. As a caveat, this involves storing the first letter of the last
name in the ID_Number field, which violates normalization rules. However, I
have struggled and searched and asked questions, and in general have spent
hours and hours trying to avoid including the first letter of the last name
in the ID_Number field, but I cannot find a solution. While I'm at it I
store the hyphen as well, although I could get around that easily enough by
doing some concatenation.
You could use something like this in a command button's Click event, or the
After Update event of the text box for the person's last name. In my test
the table is tblConcat, and the text field in which the number is stored is
ConcatName. Note that the record is saved before the code to assign the
number is run. In a multi-user environment this will help to guard against
the code assigning duplicate numbers if two different users are entering new
records at the same time for people with the same last name.
Me.Dirty = False ' Saves the record
If Me.NewRecord Then
Dim strWhere As String
Dim varResult As Variant
strWhere = "ConcatName Like """ & Left(Me.LastName, 1) & "*"""
varResult = DMax("ConcatName", "tblConcat", strWhere)
If IsNull(varResult) Then
Me.ConcatName = Left(Me.LastName, 1) & "-001"
Else
Me.ConcatName = Left(varResult, 2) & _
Format(Val(Right(varResult, 3)) + 1, "000")
End If
Me.Refresh
End If
The line:
If Me.NewRecord Then
could be something like:
If IsNull(Me.ConcatName) Then
or in some other way run the code on demand.
There are some potential problems with your numbering approach, I think.
What happens if somebody's name changes? Is the number reassigned? If the
only person with a last name beginning with "Q" has Q-001, then that
person's name changes, if the original "Q" person gets a new number, the
next person to come along with a last name beginning with "Q" will get the
number Q-001. Now two different people at different times will have the
same number. It may be best to have an Inactive field, but in any case not
to delete name or reuse numbers.
To others who are reading this thread, I would be glad to know a way to
avoid storing the first letter of the last name.