Поскольку вы из Дании, таблица, скорее всего, будет отражать годы и недели согласно ISO 8601 . Однако ни одна собственная функция VBA не вернет год и неделю даты по ISO 8601, а для дат около Нового года год часто будет отличаться от календарного года.
Таким образом, функции для возврата ISO Требуется 8601 год и неделя:
Public Const MaxWeekValue As Integer = 53
Public Const MinWeekValue As Integer = 1
Public Const MaxMonthValue As Integer = 12
Public Const MinMonthValue As Integer = 1
' Returns the ISO 8601 week of a date.
' The related ISO year is returned by ref.
'
' 2016-01-06. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function Week( _
ByVal Date1 As Date, _
Optional ByRef IsoYear As Integer) _
As Integer
Dim Month As Integer
Dim Interval As String
Dim Result As Integer
Interval = "ww"
Month = VBA.Month(Date1)
' Initially, set the ISO year to the calendar year.
IsoYear = VBA.Year(Date1)
Result = DatePart(Interval, Date1, vbMonday, vbFirstFourDays)
If Result = MaxWeekValue Then
If DatePart(Interval, DateAdd(Interval, 1, Date1), vbMonday, vbFirstFourDays) = MinWeekValue Then
' OK. The next week is the first week of the following year.
Else
' This is really the first week of the next ISO year.
' Correct for DatePart bug.
Result = MinWeekValue
End If
End If
' Adjust year where week number belongs to next or previous year.
If Month = MinMonthValue Then
If Result >= MaxWeekValue - 1 Then
' This is an early date of January belonging to the last week of the previous ISO year.
IsoYear = IsoYear - 1
End If
ElseIf Month = MaxMonthValue Then
If Result = MinWeekValue Then
' This is a late date of December belonging to the first week of the next ISO year.
IsoYear = IsoYear + 1
End If
End If
' IsoYear is returned by reference.
Week = Result
End Function
' Returns the ISO 8601 year of a date.
'
' 2016-01-06. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function YearOfWeek( _
ByVal Date1 As Date) _
As Integer
Dim IsoYear As Integer
' Get the ISO 8601 year of Date1.
Week Date1, IsoYear
YearOfWeek = IsoYear
End Function
Пример:
? Week(#2024/12/31#)
1
? YearOfWeek(#2024/12/31#)
2025
Теперь используйте их в своем запросе:
SELECT
T.Year,
T.Week,
T.Data
FROM
tblData AS T
WHERE
T.Year * 100 + T.Week < YearOfWeek(Date()) * 100 + Week(Date())
И вы можете безопасно сохранить Год в качестве имени поля, если вы добавляете к нему префикс в запросах с именем таблицы, как вы уже делаете, или заключаете его в квадратные скобки: [Year]
.