Private Function GetNextValue() As String
Dim sSQL As String
Dim rs As Recordset
sSQL = ""
sSQL = sSQL & "SELECT MAX(AutoAlpha) FROM Table1 " 'Table1 should be the table that stores your data and the AutoAlpha is the primary field that you need the auto number
Set rs = CurrentDb.OpenRecordset(sSQL)
If Right(rs(0), Len(rs(0)) - 5) < 1000000 Then
GetNextValue = "AAA" & Year(Date) & GetLeadingZeroes(Right(rs(0), Len(rs(0)) - 5))
Else
GetNextValue = "AAA" & Year(Date) & Right(rs(0), Len(rs(0)) - 5)
End If
rs.Close
Set rs = Nothing
End Function
Private Function GetLeadingZeroes(ByVal lNum As Long) As String
Dim sVal As String
sVal = lNum + 1
'Here is just keeping track of the 8-digit number by adding zeroes for those number that are less than 8 digit.
Do While Len(sVal) < 8
sVal = "0" & sVal
Loop
GetLeadingZeroes = sVal
End Function