sdawson
Registered User.
- Local time
- Today, 08:22
- Joined
- Apr 22, 2003
- Messages
- 165
I use the module below to return the week number (ISOWeekNum) for a date input in usual format (dd/mm/yy).
I now need to return the week ending date (Sunday) from an input week number in a query. That is, I input the week number and the query filters the data up to the week ending date for the week number input. Got it.
This should be simple.
Any links or help appreciated.
Ta
-----------------------------------------------------------------------
Option Compare Database
Option Explicit
Public Function ISOWeekNum(AnyDate As Date, _
Optional WhichFormat As Variant) As Integer
' WhichFormat: missing or <> 2 then returns week number,
' = 2 then YYWW
'WhichFormat = 2
Dim ThisYear As Integer
Dim PreviousYearStart As Date
Dim ThisYearStart As Date
Dim NextYearStart As Date
Dim YearNum As Integer
ThisYear = Year(AnyDate)
ThisYearStart = YearStart(ThisYear)
PreviousYearStart = YearStart(ThisYear - 1)
NextYearStart = YearStart(ThisYear + 1)
Select Case AnyDate
Case Is >= NextYearStart
ISOWeekNum = (AnyDate - NextYearStart) \ 7 + 1
YearNum = Year(AnyDate) + 1
Case Is < ThisYearStart
ISOWeekNum = (AnyDate - PreviousYearStart) \ 7 + 1
YearNum = Year(AnyDate) - 1
Case Else
ISOWeekNum = (AnyDate - ThisYearStart) \ 7 + 1
YearNum = Year(AnyDate)
End Select
If IsMissing(WhichFormat) Then
Exit Function
End If
If WhichFormat = 2 Then
ISOWeekNum = CInt(Format(Right(YearNum, 2), "00") & _
Format(ISOWeekNum, "00"))
End If
End Function
------------------------------------------------------------------------
Function YearStart(WhichYear As Integer) As Date
Dim WeekDay As Integer
Dim NewYear As Date
NewYear = DateSerial(WhichYear, 1, 1)
WeekDay = (NewYear - 2) Mod 7
If WeekDay < 4 Then
YearStart = NewYear - WeekDay
Else
YearStart = NewYear - WeekDay + 7
End If
End Function
I now need to return the week ending date (Sunday) from an input week number in a query. That is, I input the week number and the query filters the data up to the week ending date for the week number input. Got it.
This should be simple.
Any links or help appreciated.
Ta
-----------------------------------------------------------------------
Option Compare Database
Option Explicit
Public Function ISOWeekNum(AnyDate As Date, _
Optional WhichFormat As Variant) As Integer
' WhichFormat: missing or <> 2 then returns week number,
' = 2 then YYWW
'WhichFormat = 2
Dim ThisYear As Integer
Dim PreviousYearStart As Date
Dim ThisYearStart As Date
Dim NextYearStart As Date
Dim YearNum As Integer
ThisYear = Year(AnyDate)
ThisYearStart = YearStart(ThisYear)
PreviousYearStart = YearStart(ThisYear - 1)
NextYearStart = YearStart(ThisYear + 1)
Select Case AnyDate
Case Is >= NextYearStart
ISOWeekNum = (AnyDate - NextYearStart) \ 7 + 1
YearNum = Year(AnyDate) + 1
Case Is < ThisYearStart
ISOWeekNum = (AnyDate - PreviousYearStart) \ 7 + 1
YearNum = Year(AnyDate) - 1
Case Else
ISOWeekNum = (AnyDate - ThisYearStart) \ 7 + 1
YearNum = Year(AnyDate)
End Select
If IsMissing(WhichFormat) Then
Exit Function
End If
If WhichFormat = 2 Then
ISOWeekNum = CInt(Format(Right(YearNum, 2), "00") & _
Format(ISOWeekNum, "00"))
End If
End Function
------------------------------------------------------------------------
Function YearStart(WhichYear As Integer) As Date
Dim WeekDay As Integer
Dim NewYear As Date
NewYear = DateSerial(WhichYear, 1, 1)
WeekDay = (NewYear - 2) Mod 7
If WeekDay < 4 Then
YearStart = NewYear - WeekDay
Else
YearStart = NewYear - WeekDay + 7
End If
End Function