CJ, put this code in a new module, say basControl
Option Compare Database
Option Explicit
Public Function fcnGetNextSequence() as String
fcnGetNextSequence = DLookup("seqNo", "tblControl")
End Function
Public Function fcnUpdate_tblControl(Optional strStart As String)
Dim strSQL As String
Dim strLtr As String
Dim strNum As Variant
Dim strConcat As String
If Len(strStart) = 0 Then
strNum = Right(DLookup("seqno", "tblControl"), 4)
strLtr = Left(DLookup("seqno", "tblControl"), 1)
Else
strNum = Right(strStart, 4)
strLtr = Left(strStart, 1)
End If
If strNum = "9999" Then
strNum = "0001"
strLtr = Asc(strLtr) + 1
strLtr = Chr$(strLtr)
Else: strNum = strNum + 1
End If
strNum = Format(strNum, "0000")
strConcat = strLtr + strNum
strSQL = "UPDATE tblControl set seqNo = " & Chr$(39) & strConcat &
Chr$(39)
CurrentDb.Execute strSQL, dbFailOnError
End Function
Create a new table with one row and one column, no key. Call it tblControl.
The single text field is called seqno
Put a starting number in the table, say A9998, for testing.
Calling fcnNextSequence will return the value of seqNo in the table.
Calling fcnUpdate_tblControl will increment the value of seqNo in tblControl
The latter function has an optional argument; it will reset the beginning
seqNo in tblControl.
If you ever use the optional argument make sure it is entered like
call fcnUpDate_tblControl("A1234")
You might want to use this to reset the SeqNo when it reaches Z9999 !
HTH, UpRider