Patty:
Firstly the data type for the CustomerNumber column will need to be Text if
you want to store the leading zero of the year. It then depends on whether
you want the last six characters of the customer number to start from 000001
each month, or to be unique. For the former, in a form bound to the
Customers table put code along these lines in the form's BeforeInsert event
procedure:
Dim strYearMonth As String
Dim strCriteria as String
Dim varLastNumber as Variant
strYearMonth = Format(VBA.Date,"yymm")
strCriteria = "Left([CustomerNumber],4) = """ & strYearMonth & """"
varLastNumber = DMax("[CustomerNumber]", "[Customers]", strCriteria)
' add 1 to last customer for year/month, if any
If IsNull(varLastNumber) Then
Me.[CustomerNumber] = strYearMonth & "000001"
Else
Me.[CustomerNumber] = strYearMonth & _
Format(Right(varLastNumber,6)+1,"000000")
End If
If the last six characters of the customer number are unique:
Dim strYearMonth As String
Dim varLastNumber as Variant
strYearMonth = Format(VBA.Date,"yymm")
varLastNumber = DMax("[CustomerNumber]", "[Customers]")
' add 1 to last customer if any
If IsNull(varLastNumber) Then
Me.[CustomerNumber] = strYearMonth & "000001"
Else
Me.[CustomerNumber] = strYearMonth & _
Format(Right(varLastNumber,6)+1,"000000")
End If
Note that in a multi-user environment this could case conflicts if two or
more users are adding a new customer record simultaneously. Provided the
CustomerNumber column is indexed uniquely, however, this would raise an error
which could then be handled.
Ken Sheridan
Stafford, England