A modified version using one of my sample tables (AuthorsT). I commented code that is not used.
Code:
Private Sub aid_Click()
Dim NUM As Long
Dim MS As String
Dim S As String
Dim strsql As String
Dim rpt As Report
Dim aut As String
Dim auts As Integer
Dim var As Variant
Dim jvar As String 'my variable for testing
'check for authorName
If Len(Trim$(Me.authorName & "")) = 0 Then
MsgBox ("The Book's Author Cannot be Left Blank")
Exit Sub
End If
'authorname exists so check aid
If (Me.aid) & "" = "" Then
MsgBox "AID can not be null"
'adjusting AID
jvar = Format(CLng(DMax("Mid(aid, 3)", "AuthorsT") + 1), "00000") 'format adjusted for 5 digits
MsgBox "The Author ID = AU" & jvar
Me.aid = "AU" & jvar
End If
' NUM = Nz(DLookup(Mid(Me.aid, 3), "AuthorsT", "Name = '" & Me.authorName.Value & "'"), 0)
' MsgBox ("The Author ID = " & Format$(NUM, "AU0000"))
' If (NUM > 0) Then
'
' aid.Value = NUM
' Else
' MsgBox ("The author is not in the database")
' NUM = val(DMax("ID", "Authors") & "")
'
' If (NUM > 0) Then
' NUM = NUM + 1
' Else
' NUM = 1
' End If
'
' aut = Format$(NUM, "AU0000")
' MsgBox ("The Author ID = " & aut)
' aid.Value = NUM
' End If
' print status message
MsgBox "Record: '" & Me.aid & "' '" & Me.authorName & "'"
Debug.Print "Record: '" & Me.aid & "' '" & Me.authorName & "'"
End Sub
Started with 'AU00001' Bob
Added 3 records without AID
Record: 'AU00002' 'Jim'
Record: 'AU00003' 'Chris'
Record: 'AU00004' 'Ted'