使用 "Date Code" 的 Microsoft Access 表单自动生成,但总是晚 1 周
Microsoft Access Form using "Date Code" to be autogenerated but is always 1 week behind
我不精通 Access,正在尝试找出日期代码字段未正确传播的原因。字段 Screenshot of Date Code 显示为 YYWWDD(年份的最后两位数、年份的周数和工作周的日期(星期一 = 1 - 星期五 = 5)),但在过去两年中,年份的周数有一直落后 1 周。如屏幕截图中日期代码 20242 所示,这实际上是自一月第一周以来的第 25 周。
属性sheet中的代码如下:
=IIf(IsNull([DaySelected]),IIf(Len(Format(Date(),"ww",2,3))=1,Format(Date(),"yy") & "0" & Format(Date(),"ww w",2,3),Format(Date(),"yywww ",2,3)),IIf(Len(Format(Date(),"ww",2,3))=1,Format(Date(),"yy") & "0" & Format(Date(),"ww",2,3) & [DaySelected],Format(Date(),"yyww",2,3) & [DaySelected]))
如有任何帮助,我们将不胜感激。
问题是,年份是 ISO 8601 年,而不是日历年,需要一个自定义函数来获取真正的 ISO 8601 周数:
' 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
Const MaxWeekValue As Integer = 53
Const MinWeekValue As Integer = 1
Const MaxMonthValue As Integer = 12
Const MinMonthValue As Integer = 1
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
然后,构建一个简单的自定义函数来生成您的年-周-日数字:
Public Function FormatWeekDayIso8601(ByVal DaySelected As Integer) As String
Dim Result As String
Dim IsoYear As Integer
Dim IsoWeek As Integer
IsoWeek = Week(Date, IsoYear)
Result = _
CStr(IsoYear Mod 100) & _
VBA.Format(IsoWeek, String(2, "0")) & _
CStr(DaySelected)
FormatWeekDayIso8601 = Result
End Function
现在,像这样使用它:
=FormatWeekDayIso8601(Nz([DaySelected],1))
我不精通 Access,正在尝试找出日期代码字段未正确传播的原因。字段 Screenshot of Date Code 显示为 YYWWDD(年份的最后两位数、年份的周数和工作周的日期(星期一 = 1 - 星期五 = 5)),但在过去两年中,年份的周数有一直落后 1 周。如屏幕截图中日期代码 20242 所示,这实际上是自一月第一周以来的第 25 周。
属性sheet中的代码如下:
=IIf(IsNull([DaySelected]),IIf(Len(Format(Date(),"ww",2,3))=1,Format(Date(),"yy") & "0" & Format(Date(),"ww w",2,3),Format(Date(),"yywww ",2,3)),IIf(Len(Format(Date(),"ww",2,3))=1,Format(Date(),"yy") & "0" & Format(Date(),"ww",2,3) & [DaySelected],Format(Date(),"yyww",2,3) & [DaySelected]))
如有任何帮助,我们将不胜感激。
问题是,年份是 ISO 8601 年,而不是日历年,需要一个自定义函数来获取真正的 ISO 8601 周数:
' 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
Const MaxWeekValue As Integer = 53
Const MinWeekValue As Integer = 1
Const MaxMonthValue As Integer = 12
Const MinMonthValue As Integer = 1
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
然后,构建一个简单的自定义函数来生成您的年-周-日数字:
Public Function FormatWeekDayIso8601(ByVal DaySelected As Integer) As String
Dim Result As String
Dim IsoYear As Integer
Dim IsoWeek As Integer
IsoWeek = Week(Date, IsoYear)
Result = _
CStr(IsoYear Mod 100) & _
VBA.Format(IsoWeek, String(2, "0")) & _
CStr(DaySelected)
FormatWeekDayIso8601 = Result
End Function
现在,像这样使用它:
=FormatWeekDayIso8601(Nz([DaySelected],1))