J
Juan Correa
Hello,
I have these two functions (written with the help of some of you guys here)
Public Function ThisPeriod(ByVal dtmBaseDate As Date) As Date
'Finds the date of the Last Friday of the month for the date entered
Dim intCurrMonth As Integer
Dim intNextMonth As Integer
Dim intNextYear As Integer
Dim blnAllDone As Boolean
' Find the Current and Next Months
intCurrMonth = Month(dtmBaseDate)
intNextYear = Year(dtmBaseDate) + 1
intNextMonth = intCurrMonth + 1
' Find the Friday for the date passed
dtmBaseDate = DateAdd("d", vbFriday - DatePart("w", dtmBaseDate),
dtmBaseDate)
If Month(dtmBaseDate) = intNextMonth Then
'The Friday for the date entered is in the next month
'so subtract a week and it will be the last Friday
ThisPeriod = DateAdd("ww", -1, dtmBaseDate)
Exit Function
End If
If Month(dtmBaseDate) <> intCurrMonth Then
'The Friday for the date entered is in the previous month
'so add a week to get back to current month
'Used <> instead of < because in Jan(1) you can end up in Dec(12)
dtmBaseDate = DateAdd("ww", 1, dtmBaseDate)
End If
blnAllDone = False
Do Until blnAllDone
dtmBaseDate = DateAdd("ww", 1, dtmBaseDate)
blnAllDone = Month(dtmBaseDate) = intNextMonth Or _
Year(dtmBaseDate) = intNextYear
Loop 'Until blnAllDone
ThisPeriod = DateAdd("ww", -1, dtmBaseDate)
End Function
Public Function NextPeriod(InvDate As Date) As Date
Dim dtmPeriodEnd As Date
dtmPeriodEnd = ThisPeriod(InvDate)
NextPeriod = ThisPeriod(DateSerial(Year(dtmPeriodEnd),
Month(dtmPeriodEnd) + 1, 1))
End Function
And using those two functions I have created a calculated field in a query
that looks like this:
lastFriday:
IIf([tbl_invoices]![invoiceDate]<=ThisPeriod([tbl_invoices]![invoiceDate]),ThisPeriod([tbl_invoices]![invoiceDate]),NextPeriod([tbl_invoices]![invoiceDate]))
What that calculated field outputs is the "Last Friday" associated with that
invoice date.
Here are the conditions:
1. If invoice date is before Last Friday date then use Last Friday date.
2. If invoice date is after Last Friday date then use Last Friday date of
next month.
The whole thing works except in the following instance:
When the month is December and the invoice date is after the "Last Friday"
for December.
Example:
December 2008 ---> Last Friday for that period is 12/26/2008
If I have an invoice date 12/27/2008 through 12/31/2008 the calculatd field
fails and produces this result: 01/09/2009.
Again this only happens in the month of December. Any other months where
the invoice date is after the Last Friday for that particular month will
produce the desired result (The last Friday of the next month).
I'm pretty sure the error comes from the NextPeriod() function, but I can't
figure out the correction.
Any help on this would be greatly appreciated.
Cheers
Sebastian
I have these two functions (written with the help of some of you guys here)
Public Function ThisPeriod(ByVal dtmBaseDate As Date) As Date
'Finds the date of the Last Friday of the month for the date entered
Dim intCurrMonth As Integer
Dim intNextMonth As Integer
Dim intNextYear As Integer
Dim blnAllDone As Boolean
' Find the Current and Next Months
intCurrMonth = Month(dtmBaseDate)
intNextYear = Year(dtmBaseDate) + 1
intNextMonth = intCurrMonth + 1
' Find the Friday for the date passed
dtmBaseDate = DateAdd("d", vbFriday - DatePart("w", dtmBaseDate),
dtmBaseDate)
If Month(dtmBaseDate) = intNextMonth Then
'The Friday for the date entered is in the next month
'so subtract a week and it will be the last Friday
ThisPeriod = DateAdd("ww", -1, dtmBaseDate)
Exit Function
End If
If Month(dtmBaseDate) <> intCurrMonth Then
'The Friday for the date entered is in the previous month
'so add a week to get back to current month
'Used <> instead of < because in Jan(1) you can end up in Dec(12)
dtmBaseDate = DateAdd("ww", 1, dtmBaseDate)
End If
blnAllDone = False
Do Until blnAllDone
dtmBaseDate = DateAdd("ww", 1, dtmBaseDate)
blnAllDone = Month(dtmBaseDate) = intNextMonth Or _
Year(dtmBaseDate) = intNextYear
Loop 'Until blnAllDone
ThisPeriod = DateAdd("ww", -1, dtmBaseDate)
End Function
Public Function NextPeriod(InvDate As Date) As Date
Dim dtmPeriodEnd As Date
dtmPeriodEnd = ThisPeriod(InvDate)
NextPeriod = ThisPeriod(DateSerial(Year(dtmPeriodEnd),
Month(dtmPeriodEnd) + 1, 1))
End Function
And using those two functions I have created a calculated field in a query
that looks like this:
lastFriday:
IIf([tbl_invoices]![invoiceDate]<=ThisPeriod([tbl_invoices]![invoiceDate]),ThisPeriod([tbl_invoices]![invoiceDate]),NextPeriod([tbl_invoices]![invoiceDate]))
What that calculated field outputs is the "Last Friday" associated with that
invoice date.
Here are the conditions:
1. If invoice date is before Last Friday date then use Last Friday date.
2. If invoice date is after Last Friday date then use Last Friday date of
next month.
The whole thing works except in the following instance:
When the month is December and the invoice date is after the "Last Friday"
for December.
Example:
December 2008 ---> Last Friday for that period is 12/26/2008
If I have an invoice date 12/27/2008 through 12/31/2008 the calculatd field
fails and produces this result: 01/09/2009.
Again this only happens in the month of December. Any other months where
the invoice date is after the Last Friday for that particular month will
produce the desired result (The last Friday of the next month).
I'm pretty sure the error comes from the NextPeriod() function, but I can't
figure out the correction.
Any help on this would be greatly appreciated.
Cheers
Sebastian