Hello,
I have the below function that works correctly. However, I would like the "Last_Nbr_Assigned" value to start at 0000 and increment by 1. So the very first number assigned would be 0001, then 0002, then 0003, and so on.
This number is automatically assigned so I haven't had luck with the validation properties. And if manually enter 0001 for the "Last_Nbr_Assigned", it will automatically cut off the 3 zeros, leaving me with 2 instead of the desired 0002.
Any suggestions?
I have the below function that works correctly. However, I would like the "Last_Nbr_Assigned" value to start at 0000 and increment by 1. So the very first number assigned would be 0001, then 0002, then 0003, and so on.
This number is automatically assigned so I haven't had luck with the validation properties. And if manually enter 0001 for the "Last_Nbr_Assigned", it will automatically cut off the 3 zeros, leaving me with 2 instead of the desired 0002.
Any suggestions?
Code:
Function NewRF() As String
Dim startRange_Var As Long, lastNum_Var As Long
On Error GoTo Err_Execute
'Retrieve last number assigned for RF
groupname_Var = Nz(DLookup("Group_Name", "Codes"), 0)
codeDesc_Var = Nz(DLookup("Code_Desc", "Codes"), 0)
lastNum_Var = Nz(DLookup("Last_Nbr_Assigned", "Codes"), 0)
ycodeDesc_Var = Nz(DLookup("YearValue", "Codes"), 0)
Set db = CurrentDb()
Set rstsource = db.OpenRecordset("Codes", dbOpenDynaset)
'If no records were found, create a new RF in the Codes table
'and set initial value to 1
If lastNum_Var = -1 Then
groupname_Var = Nz(DLookup("Group_Name", "Codes"), 0)
codeDesc_Var = Nz(DLookup("Code_Desc", "Codes"), 0)
lastNum_Var = Nz(DLookup("Last_Nbr_Assigned", "Codes"), 0)
ycodeDesc_Var = Nz(DLookup("YearValue", "Codes"), 0)
Set db = CurrentDb()
Set rstsource = db.OpenRecordset("Codes", dbOpenDynaset)
'New RF is formatted as "RF-1", if starting from scratch
LNewRF = codeDesc_Var & "-" & lastNum_Var
Else
'Determine new RF
'New RF is formatted as "RF-1"
LNewRF = groupname_Var & "-" & codeDesc_Var & "-" & lastNum_Var & "-" & ycodeDesc_Var
'Debug.Print CLng(lastNum_Var))
'Increment counter in Codes table by 1
LUpdate = "UPDATE Codes SET Last_Nbr_Assigned = " & lastNum_Var + 1
CurrentDb.Execute LUpdate, dbFailOnError
End If
NewRF = LNewRF
Exit Function
Err_Execute:
'An error occurred, return blank string
NewRF = ""
Call MsgBox("An error occurred while trying to determine the next RF to assign." & Chr(10) & Err.Description, vbCritical)
End Function