Hi
I've created (and stole!) the 3 functions below, the first 2 taken from Tek-Tips to work out the Start and End of Months the 3rd Created by me to work out how much a customer should be billed for storage based on Total Days something has been stored in that month (MonthOfCharge) multiplied by a Rate (ChargeRate) that comes from the customers record.
e.g. A bill for May 2004 with a Customer Rate of 3, where storage is from (UK Date Format) 20/02/04 to 31/07/04 would incur a cost of 93 (31 days * Rate of 3)
It all works perfectly in the SubForm and the Subsequent Report for existing records but here is the problem...
When entering a new record in the SubForm the Calc Field shows #Error# Until the Values For FirstDate and LastDate are completed Thus creating the TotalDays used in the final calculation.
I have tried IIF's NZ's but I can't seem to get them in the right place or working!
Please Help, my head is pickled and Englands defeat on Penalties (again!) in Euro 2004 last night and subsequent drowning of sorrows hasn't helped much either!
Here's the code:
Regards
Brockers
Instructor
New Horizons Manchester England
I've created (and stole!) the 3 functions below, the first 2 taken from Tek-Tips to work out the Start and End of Months the 3rd Created by me to work out how much a customer should be billed for storage based on Total Days something has been stored in that month (MonthOfCharge) multiplied by a Rate (ChargeRate) that comes from the customers record.
e.g. A bill for May 2004 with a Customer Rate of 3, where storage is from (UK Date Format) 20/02/04 to 31/07/04 would incur a cost of 93 (31 days * Rate of 3)
It all works perfectly in the SubForm and the Subsequent Report for existing records but here is the problem...
When entering a new record in the SubForm the Calc Field shows #Error# Until the Values For FirstDate and LastDate are completed Thus creating the TotalDays used in the final calculation.
I have tried IIF's NZ's but I can't seem to get them in the right place or working!
Please Help, my head is pickled and Englands defeat on Penalties (again!) in Euro 2004 last night and subsequent drowning of sorrows hasn't helped much either!
Here's the code:
Code:
Public Function StartOfMonth(Inputdate As Date) As Date
If IsDate(Inputdate) Then
StartOfMonth = DateSerial(Year(Inputdate), Month(Inputdate), 1)
End If
End Function
Code:
Public Function EndOfMonth(Inputdate As Date) As Date
EndOfMonth = DateSerial(Year(Inputdate), Month(Inputdate) + 1, 0)
End Function
Code:
Public Function MonthlyCharge(MonthOfCharge As Date, FirstDate As Date, LastDate As Date, ChargeRate As Integer) As Currency
Dim TotalDays As Long
Dim BillStart As Date
Dim BillEnd As Date
BillStart = IIf(FirstDate < StartOfMonth(MonthOfCharge), StartOfMonth(MonthOfCharge), FirstDate)
BillEnd = IIf(LastDate > EndOfMonth(MonthOfCharge), EndOfMonth(MonthOfCharge), LastDate)
TotalDays = DateDiff("d", BillStart, BillEnd) + 1
MonthlyCharge = IIf(TotalDays <= 0, 0, TotalDays * ChargeRate)
End Function
Regards
Brockers
Instructor
New Horizons Manchester England