Since editing, or changes may occur to the name, then you would have to
"test/run" the code for both the firstname field, and the lastname field.
So, in the after update event of the F_Name, you could put:
Call MakeCref
And, in the after update event of the S_Name field you put:
Call MakeRef
Then, in the forms module code your code would be:
Public Sub MakeRef()
Dim strWhere As String
Dim lngNextNum As Long
Dim s_name2 As String
Dim F_name2 As String
If Len(Nz(Me!F_Name, "")) < 2 Then
' blank First name...don't do anything
Exit Sub
End If
If Len(Nz(Me!S_Name, "")) < 2 Then
' blank surname... don't do anything
Exit Sub
End If
s_name2 = Left(Me!S_Name, 2)
F_name2 = Left(Me!F_Name, 2)
strWhere = "(S_name like '" & s_name2 & "*')" & _
" and (F_name like '" & F_name2 & "*')"
If IsNull(Me!ID) = False Then
' this not a new reocrd, so we must exclude this reocrd
strWhere = strWhere & " and (id <> " & Me!ID & ")"
End If
lngNextNum = DCount("*", "tblCustomers", strWhere) + 1
Me.C_ref = s_name2 & F_name2 & Format(lngNextNum, "00")
End Sub
The above code is air code, and not tested..but it should be quite close to
what you need. Make sure there is a index on firstname, and surename fields
for performance...
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(E-Mail Removed)
http://www.members.shaw.ca/AlbertKallal