Well here it goes. I would be eternally grateful for any insight into where i am going wrong.
Background
I am creating a database for collecting details of commercial properties. The essential data is the Lease start date, Lease length and a review every 5 years into that lease length.
Issue
My trouble is that i have to calculate the lease end date, being a certain number of years after the commencement. Easy yes, but it has to consider the fact that there are reviews on 5 yearly intervals and from this point onwards. So, the database has to calculate the next future review and ignore the previous ones.
Example
Lease start date: 1st August 1990
Lease length: 30 years
Review period: 5 years
So, according to the above, the next review is in 1st August 2005 as the previous reviews (1995 & 2000) have elapsed.
This is the date that i want inserted into a field in the form, when the previous fields (Lease Start Date, Term & Review Period) have been entered.
Efforts to Date
Up till now, i have tried creating a macro with the calculation but i couldnt get all the formulae to work within the expression builder correctly. Therefore, i moved to a module that was called by a macro on an Update/Change/Click event for the input field boxes. This seemed the best possible progress so far.
The calculation for the above is as follows: -
Rent Review Date = (Term - ((Lease Start Date + Term) - Today's Year)) + Lease Start Date
Translating this into a function, i created this: -
--------------------------Start--------------------------
Function RRMacro()
On Error GoTo Err_RRMacro
Dim FullTerm, FullTermStr, RemTermStr, BalanceTermStr, BalanceTerm, NextRrDate
FullTerm = DateAdd("yyyy", [Term], [LeaseStartDate])
FullTermStr = Format(FullTerm, "yyyy")
RemTermStr = FullTermStr - Year(Date)
BalanceTermStr = [Term] - RemTermStr
BalanceTerm = Format(BalanceTermStr, "yyyy")
NextRrDate = BalanceTerm + Year([LeaseStartDate])
[NextReviewDate] = NextRrDate
GoTo Exit_RRMacro
Err_RRMacro:
MsgBox Err.Description
Resume Exit_RRMacro
Exit_RRMacro:
Exit Function
End Function
--------------------------End--------------------------
Observations
So, what i started with was to do a straight forward calculation and provide the result into a field box. The problem i had was the LeaseStartDate (Long Date) was added to the Term (General Number) and this produced odd answers. I soon discovered this was because they were not in the same formats. Access added the term to the date as a number of days and not years. So, i multiplied this by 365, as above.
However, i also realised i needed to convert to strings for the calculation. It started to go wrong from "BalanceTerm=......" where i tried to convert the string back into the date in a year format. Also, with "NextRrDate =......" the resultant calculation became rather messed up.
Basically, it didnt work. The dates it gave where innacurate and this had to be down to the fact that i was working with the years ("yyyy") and cut off the leading day and month.
Result?
So, the question is: how do i carry the calculation as per the formula above using dates throughout? There is something i'm missing but i just cant seem to solve it?
I would be so very grateful for any insight. Apologies for the long winded post.
Thanks again
Popolou
Background
I am creating a database for collecting details of commercial properties. The essential data is the Lease start date, Lease length and a review every 5 years into that lease length.
Issue
My trouble is that i have to calculate the lease end date, being a certain number of years after the commencement. Easy yes, but it has to consider the fact that there are reviews on 5 yearly intervals and from this point onwards. So, the database has to calculate the next future review and ignore the previous ones.
Example
Lease start date: 1st August 1990
Lease length: 30 years
Review period: 5 years
So, according to the above, the next review is in 1st August 2005 as the previous reviews (1995 & 2000) have elapsed.
This is the date that i want inserted into a field in the form, when the previous fields (Lease Start Date, Term & Review Period) have been entered.
Efforts to Date
Up till now, i have tried creating a macro with the calculation but i couldnt get all the formulae to work within the expression builder correctly. Therefore, i moved to a module that was called by a macro on an Update/Change/Click event for the input field boxes. This seemed the best possible progress so far.
The calculation for the above is as follows: -
Rent Review Date = (Term - ((Lease Start Date + Term) - Today's Year)) + Lease Start Date
Translating this into a function, i created this: -
--------------------------Start--------------------------
Function RRMacro()
On Error GoTo Err_RRMacro
Dim FullTerm, FullTermStr, RemTermStr, BalanceTermStr, BalanceTerm, NextRrDate
FullTerm = DateAdd("yyyy", [Term], [LeaseStartDate])
FullTermStr = Format(FullTerm, "yyyy")
RemTermStr = FullTermStr - Year(Date)
BalanceTermStr = [Term] - RemTermStr
BalanceTerm = Format(BalanceTermStr, "yyyy")
NextRrDate = BalanceTerm + Year([LeaseStartDate])
[NextReviewDate] = NextRrDate
GoTo Exit_RRMacro
Err_RRMacro:
MsgBox Err.Description
Resume Exit_RRMacro
Exit_RRMacro:
Exit Function
End Function
--------------------------End--------------------------
Observations
So, what i started with was to do a straight forward calculation and provide the result into a field box. The problem i had was the LeaseStartDate (Long Date) was added to the Term (General Number) and this produced odd answers. I soon discovered this was because they were not in the same formats. Access added the term to the date as a number of days and not years. So, i multiplied this by 365, as above.
However, i also realised i needed to convert to strings for the calculation. It started to go wrong from "BalanceTerm=......" where i tried to convert the string back into the date in a year format. Also, with "NextRrDate =......" the resultant calculation became rather messed up.
Basically, it didnt work. The dates it gave where innacurate and this had to be down to the fact that i was working with the years ("yyyy") and cut off the leading day and month.
Result?
So, the question is: how do i carry the calculation as per the formula above using dates throughout? There is something i'm missing but i just cant seem to solve it?
I would be so very grateful for any insight. Apologies for the long winded post.
Thanks again
Popolou
Last edited: