I must create a sequential case number based on the previous case in the series. I have multiple series, e.g., AA, BA, XYZ. Each series is coded by two-digit year, and sequential number. Some series have 4 digit numbers and some series have 3 digit numbers, so the first AA case for 2019 would be AA190001, while the first for the BA series would be BA19001. Add to that some series are 2 letters, and some are 3 (e.g., AA vs. XYZ) and you have a mess.
Assume I need to add a new case to the AA series, and the last one was AA190500. The user provides the series (AA) and year (2019). I concatenate as AA19 as a prefix and then use it to filter a sorted query of cases, and grab the last with a call to DMax()
Once I have the last case number, I use the length of my prefix (which depending on the series is 4 or 5 characters) to get the sequential case number.
In my example above I get 0500, but because this has to be numeric to increment, I lose the leading 0, thus when I concatenate my prefix and the new number, I’m a number short if there was a preceding 0, and my new number comes out as AA19501.
My question is, how can I make sure to retain one or more leading zeroes if they exist?
In an effort to keep this short I’ve cut out a lot of implicit steps (declaring variables, incrementing the number by one) etc. but all the steps work. I just need to be able to keep that leading 0. If further reference is needed, see below.
Additional notes that may not be needed:
At the most extreme, e.g., at the beginning of a new year when you have a number like AA200002 the created value would be AA203, so I need it to be flexible enough to account for this if possible. I’ll embed the entire routine in an If() statement to handle the unusual situation when the year turns over and the first annual record (-001) needs to be added.
As further background, I’m working with a very old database with plenty of problems – bad table structure, difficulty with setting up unambiguous relationships, fuzzy data entry protocols through the years. I’m charged with making the best of the bad situation for the next 2 years as we bring online a modern up to date system that resolves most of these historic issues. I’m not able to make many substantial changes to the underlying structure, so my goal is to clean up the process for daily users via forms and under-the-hood mechanics that maintain data integrity for this short life expectancy. This was driven by legislative changes in the programs we administer and the lag-time involved in adding these programs to the new system in the later builds.
Assume I need to add a new case to the AA series, and the last one was AA190500. The user provides the series (AA) and year (2019). I concatenate as AA19 as a prefix and then use it to filter a sorted query of cases, and grab the last with a call to DMax()
Once I have the last case number, I use the length of my prefix (which depending on the series is 4 or 5 characters) to get the sequential case number.
In my example above I get 0500, but because this has to be numeric to increment, I lose the leading 0, thus when I concatenate my prefix and the new number, I’m a number short if there was a preceding 0, and my new number comes out as AA19501.
My question is, how can I make sure to retain one or more leading zeroes if they exist?
In an effort to keep this short I’ve cut out a lot of implicit steps (declaring variables, incrementing the number by one) etc. but all the steps work. I just need to be able to keep that leading 0. If further reference is needed, see below.
Code:
[SIZE=3][FONT=Calibri] Dim pref As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Dim lastCase As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Dim L As Integer[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Dim csNum As Integer[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] Dim nxtCase As String[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] ‘create prefix[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] pref = Me.cboCaseType.Column(1) & Right(Me.txtCurrentYr, 2) & "*"[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] ‘using prefix as a filter, get the most recent case number[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] lastCase = DMax("[IncidentNumber]", "qry_listCases")[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] ‘get the length of prefix (minus the asterisk wildcard)[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] L = Len(pref) – 1[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] ‘pull the sequential case number and increment it[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] csNum = Right(lastCase, Len(lastCase) - L) + 1[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] ‘concatenate the prefix and incremented value[/FONT][/SIZE]
[SIZE=3][FONT=Calibri] nxtCase = Left(pref, Len(pref) - 1) & csNum[/FONT][/SIZE]
At the most extreme, e.g., at the beginning of a new year when you have a number like AA200002 the created value would be AA203, so I need it to be flexible enough to account for this if possible. I’ll embed the entire routine in an If() statement to handle the unusual situation when the year turns over and the first annual record (-001) needs to be added.
As further background, I’m working with a very old database with plenty of problems – bad table structure, difficulty with setting up unambiguous relationships, fuzzy data entry protocols through the years. I’m charged with making the best of the bad situation for the next 2 years as we bring online a modern up to date system that resolves most of these historic issues. I’m not able to make many substantial changes to the underlying structure, so my goal is to clean up the process for daily users via forms and under-the-hood mechanics that maintain data integrity for this short life expectancy. This was driven by legislative changes in the programs we administer and the lag-time involved in adding these programs to the new system in the later builds.