Generating a project number based on other fields (1 Viewer)

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
I'm sure this can be done I'm just struggling to think out the process. (I've been out of access for some time & relearning)
I want to concatenate some fields based on elements of my form. STATE=AUTONUMBER+FINANCIAL YEAR
From the the job entered date which self calculates I can get Financial (fiscal) year.
Once the user selects a client (ClientID in main table links to a client table that has "state") I can populate state
The number is the tricky one... What we want to do is the first job in 2020 is STATE1_2020 (or Say QLD01_2020), then they go along like that until it is the next financial year, then the number resets to QLD01_2021...
I want to lookup the previous value from the last record, then check if the Financial year is still the same, then +1. If the Financial year is diff to the last record start at 1.

Then I can concatenate the fields to calculate the unique project number.
BTW this numbering system was not my idea! :rolleyes:
 

GinaWhipp

AWF VIP
Local time
Today, 04:48
Joined
Jun 21, 2011
Messages
5,899
Hmm, perhaps this will give you some ideas...

That said, you can still use Autonumber as your Primary Key keeping that hidden and generate your custom number for show. I do this when a Client has a request for a custom numbering system. The database (and you) get what you need and the Client (or boss) gets what they want.
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
Thanks @GinaWhipp . The client wants the numbers to start again at the start of a new financial year (July 1 in Australia) So I cant even really use autonumber. Im thinking I need to check the FY is the same as the last record, then if it is add one to the numerical field, if not start at 1 again
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
This is what I have so far and it doesn't work! I still need one more field to add to the project number calculation. I am not sure if checking the prev record is right?


'Calculate fields required to generate project number

Dim PrjIDNo As Single
Dim FY As String

'Financial Year
Me.FinancialYear = IIf(Month(Date) <= 6, Year(Date), Year(Date) + 1)

'Autonumber check if current year by checking the previous job, or start renumbering from 1 if its a new FY
DoCmd.GoToRecord , , acPrevious
PrjIDNo = ID

DoCmd.GoToRecord , , acNext
If FY = Me.FinancialYear Then Me.ID = PrjIDNo + 1
Else
Me.ID = 1

'Convert Project number to a string to concatenate
Dim PrjIDNo2 As String
PrjIDNo2 = Me.ID

ProjectNumber = Me.FinancialYear & "_" & PrjIDNo2
 

GinaWhipp

AWF VIP
Local time
Today, 04:48
Joined
Jun 21, 2011
Messages
5,899
Why are you checking the previous record? Wouldn't you just need a DMax()?
 

plog

Banishment Pending
Local time
Today, 03:48
Joined
May 11, 2011
Messages
11,643
... it doesn't work!

Make that statement less vague please; tell us how you know that. Unexpected results? Error message? Caused your desk to catch fire?

My educated guess is it throws an error because you assign the variable ID to PrjIDNo, but I don't see where ID gets assigned a value. After you fix that it should give unexpected results because you assign all the parts of the number you want to construct to the ProjectNumber variable but do nothing with the ProjectNumber variable.

Now for the bad news, your approach is entirely wrong. You can't rely on the prior record of the form to be the record you need to reference to determine the next ProjectNumber.

This is going to require a DMax call (https://www.techonthenet.com/access/functions/domain/dmax.php). Get the highest value in the table for the year, if null use 1 otherwise use the number +1
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
@plog if only it delivered a fine wine... It just did nothing, zip zero.
You actually answered what I was going to say to @GinaWhipp I thought it would just find the Max and it could be higher in another FY, so you are saying I can do that by checking the highest number in the FY? :rolleyes:
 

plog

Banishment Pending
Local time
Today, 03:48
Joined
May 11, 2011
Messages
11,643
Yes, pass it the criteria as the last argument (like a DLookup). Check the link for a reference
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
So check the Financial year is current first, if its not just calculate the field to "1", else DMax + 1...
Actually it might be easier to see if the new financial year is unique and if so start at one... Is there a way to look up that? Unfortunately the data that came across means the financial year is string because some values were 2017/2018
 
Last edited:

plog

Banishment Pending
Local time
Today, 03:48
Joined
May 11, 2011
Messages
11,643
I don't know why that's unfortunate or changes things. Determine whatever the current fiscal year is and format it properly and then pass it in the criteria of the DMax.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:48
Joined
May 21, 2018
Messages
8,527
financial year is string because some values were 2017/2018
If I understand then the dmax is something like
Code:
Function GetFinYear(dt As Date, Optional StartMonth As Integer = 10) As String
If Month(dt) >= StartMonth Then
   GetFinYear = (Year(dt) + 1)
Else
   GetFinYear = Year(dt)
End If
End Function

Function GetMaxNumber () as long
 getMaxNumber = nz(dmax("projectNumber", "tblProjects", "InStr(CStr(GetFinYear(Date)), [fiscalYear]) > 0",1)
End Sub
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
If I understand then the dmax is something like
Code:
Function GetFinYear(dt As Date, Optional StartMonth As Integer = 10) As String
If Month(dt) >= StartMonth Then
   GetFinYear = (Year(dt) + 1)
Else
   GetFinYear = Year(dt)
End If
End Function

Function GetMaxNumber () as long
getMaxNumber = nz(dmax("projectNumber", "tblProjects", "InStr(CStr(GetFinYear(Date)), [fiscalYear]) > 0",1)
End Sub

@MajP I am having trouble understanding this with my limited access understanding.
If I already have the financial year in the table as calculating when the job opens do I need the first function?

The other issue is as well is that the ID field will start again with each new FinancialYear
So it may be as below... IF the new job is the first job in that FinancialYear the ID needs to restart at 1. If its the same FinancialYear as the last record its the last ID +1

IDFinancialYear
12018
22018
12019
22019
32019
12020
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
So could I use something like this. I have gotten around to most of the questions I had re this one and I feel like I am almost there

I have checked and it does return the last record financial year, Im trying to get this to tell me the highgtest number in that FY


'Check the Financial year on the last record & then check the ID
Dim LastRecFY As Long
Dim LastRecID As Long

LastRecFY = DLast("FinancialYear", "tblJobManagement")
LastRecID = DMax("ID", "tblJobManagement", "FinancialYear = LastRecFY")

'Message to check result is right - delete later
MsgBox "the Last ID is " & LastRecID

'If we are still in the same financial year as the last job add one to the ID, if not restart ID at one
If Me.FinancialYear = LastRecFY Then
Me.ID = LastRecID + 1
Else
Me.ID = 1
End If
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
I wonder if I need to create a recordset or a group (of the current financial year) to run the Dmax on
 

plog

Banishment Pending
Local time
Today, 03:48
Joined
May 11, 2011
Messages
11,643
Code:
'Check the Financial year on the last record & then check the ID

WHy do you need the Financial year of the last record? Can't you determine the finanical year you want to use based on the date? Is it possible the last record was of a diferent financial year? Further, the If/Else section uses the financial year of the form. Just use that initially.

Code:
LastRecID = DMax("ID", "tblJobManagement", "FinancialYear = LastRecFY")

This will do nothing because you haven't escaped yoru variable correctly. Look what you did with the MsgBox line and do the same thing here.


I wonder if I need to create a recordset or a group (of the current financial year) to run the Dmax on

No.
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
Code:
'Check the Financial year on the last record & then check the ID

WHy do you need the Financial year of the last record? Can't you determine the finanical year you want to use based on the date? Is it possible the last record was of a diferent financial year? Further, the If/Else section uses the financial year of the form. Just use that initially.

Code:
LastRecID = DMax("ID", "tblJobManagement", "FinancialYear = LastRecFY")

This will do nothing because you haven't escaped yoru variable correctly. Look what you did with the MsgBox line and do the same thing here.




No.
I need the financial year of the last job to determine if the numbering starts again. The job numbers are statenumber_financialyear. So if the last job was QLD2_2019 and its still 2019 the next job would be QLD3_2019 but if its now 2020 the job would be QLD1_2020
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
Code:
'Check the Financial year on the last record & then check the ID

WHy do you need the Financial year of the last record? Can't you determine the finanical year you want to use based on the date? Is it possible the last record was of a diferent financial year? Further, the If/Else section uses the financial year of the form. Just use that initially.

Code:
LastRecID = DMax("ID", "tblJobManagement", "FinancialYear = LastRecFY")

This will do nothing because you haven't escaped yoru variable correctly. Look what you did with the MsgBox line and do the same thing here.




No.
Im also not sure what you mean about the variable sorry for being thick.
 

plog

Banishment Pending
Local time
Today, 03:48
Joined
May 11, 2011
Messages
11,643
I need the financial year of the last job to determine if the numbering starts again. The job numbers are statenumber_financialyear. So if the last job was QLD2_2019 and its still 2019 the next job would be QLD3_2019 but if its now 2020 the job would be QLD1_2020

No you don't. This does not require looking up any financial year, that piece of data is something you have going in.

Im also not sure what you mean about the variable sorry for being thick.

Yes you are, especially when I gave you a line of code to look at as an example. Did you look at that line of code?
 

vegemite

Registered User.
Local time
Today, 18:48
Joined
Aug 5, 2019
Messages
64
No you don't. This does not require looking up any financial year, that piece of data is something you have going in.



Yes you are, especially when I gave you a line of code to look at as an example. Did you look at that line of code?
Clearly I am not as brilliant as you Plog. I did look at the example and still didn't understand. Thick but not rude.
 
Last edited:

Users who are viewing this thread

Top Bottom