This function is supposed to auto generate an alphanumeric primary key.
My form is named F_Claim and has a textbox named noOfClaims that determines the number of keys to be generated.
In case of noOfClaims = 1 the format of the key would be as follows ("CLM/GC/0001/2010")
While in case of noOfClaims > 1 the format of the key should be ("CLM/GC/0001/2010A"),("CLM/GC/0001/2010B"),("CLM/GC/0001/2010C") and so on by keeping the autonumber fixed and incrementing the letter only.
I have to mention that the table T_M_AutoGenerate has 3 columns, [Type], [LastNumberAssigned] and [Letter].
[Type] stores "GC"
[LastNumberAssigned] stores the 0001 part of the claimNumber
[Letter] stores the letter 'A' to be incremented in case of noOfClaims > 1
The function works perfectly when the noOfClaims = 1, but it fails when the noOfClaims > 1
here's my code
My form is named F_Claim and has a textbox named noOfClaims that determines the number of keys to be generated.
In case of noOfClaims = 1 the format of the key would be as follows ("CLM/GC/0001/2010")
While in case of noOfClaims > 1 the format of the key should be ("CLM/GC/0001/2010A"),("CLM/GC/0001/2010B"),("CLM/GC/0001/2010C") and so on by keeping the autonumber fixed and incrementing the letter only.
I have to mention that the table T_M_AutoGenerate has 3 columns, [Type], [LastNumberAssigned] and [Letter].
[Type] stores "GC"
[LastNumberAssigned] stores the 0001 part of the claimNumber
[Letter] stores the letter 'A' to be incremented in case of noOfClaims > 1
The function works perfectly when the noOfClaims = 1, but it fails when the noOfClaims > 1
here's my code
Code:
Function NewClaimNo(pValue As String, noofClms As Integer, year As Integer) As String
Dim db As Database
Dim LSQL As String
Dim LUpdate As String
Dim LInsert As String
Dim Lrs As DAO.Recordset
Dim LNewClaimNo As String
Dim CLM As String
Dim Slash As String
Dim sLetter As String
On Error GoTo Err_Execute
Set db = CurrentDb()
CLM = "CLM"
Slash = "/"
sLetter = "A"
'Retrieve last number assigned for BoxesReceived
LSQL = "Select * from T_M_AutoClaimNo"
LSQL = LSQL & " where GroupTypeAbb = '" & pValue & "'"
Set Lrs = db.OpenRecordset(LSQL)
'****** if no of claim = 1
'If no records were found, create a new pValue in the T_M_AutoClaimNo table
'and set initial value to 1
If Lrs.EOF = True Then
LInsert = "Insert into T_M_AutoClaimNo (GroupTypeAbb, LastNumberAssigned)"
LInsert = LInsert & " values "
LInsert = LInsert & "('" & pValue & "', 1)"
db.Execute LInsert, dbFailOnError
'New Claim No is formatted as "CLM/GC/0001/", for example
LNewClaimNo = CLM & Slash & pValue & Slash & Format(1, "0000") & Slash & year
Else
'Determine new ClaimNo
'New ClaimNo is formatted as "CLM/GC/0001/", for example
LNewClaimNo = CLM & Slash & pValue & Slash & Format(Lrs("LastNumberAssigned") + 1, "0000") & Slash & year
'Increment counter in T_M_AutoClaimNo table by 1
LUpdate = "Update T_M_AutoClaimNo"
LUpdate = LUpdate & " set LastNumberAssigned = " & Lrs("LastNumberAssigned") + 1
LUpdate = LUpdate & " where GroupTypeAbb = '" & pValue & "'"
db.Execute LUpdate, dbFailOnError
End If
'if no of claim > 1
If noofClms > 1 Then
'If no records were found, create a new pValue in the T_M_AutoClaimNo table
'and set initial value to 1
If Lrs.EOF = True Then
LInsert = "Insert into T_M_AutoClaimNo (GroupTypeAbb, LastNumberAssigned, Letter)"
LInsert = LInsert & " values "
LInsert = LInsert & "('" & pValue & "', 1, '" & sLetter & "')"
db.Execute LInsert, dbFailOnError
'New Claim No is formatted as "CLM/GC/0001/A", for example
LNewClaimNo = CLM & Slash & pValue & Slash & Format(1, "0000") & Slash & year & Lrs("letter")
Else
'Determine new ClaimNo
'New ClaimNo is formatted as "CLM/GC/0001/A", for example
LNewClaimNo = CLM & Slash & pValue & Slash & Format(Lrs("LastNumberAssigned"), "0000") & Slash & year & Lrs("letter")
'Increment counter in T_M_AutoClaimNo table by 1
LUpdate = "Update T_M_AutoClaimNo"
LUpdate = LUpdate & " set LastNumberAssigned = " & Lrs("LastNumberAssigned")
' LUpdate = LUpdate & " set Letter = " & Lrs(Chr(Asc("Letter") + 1))
LUpdate = LUpdate & " where GroupTypeAbb = '" & pValue & "'"
db.Execute LUpdate, dbFailOnError
End If
End If
Lrs.Close
Set Lrs = Nothing
Set db = Nothing
NewClaimNo = LNewClaimNo
Exit Function
Err_Execute:
'An error occurred, return blank string
NewClaimNo = ""
MsgBox "An error occurred while trying to determine the next ClaimNo to assign."
End Function