简体   繁体   English

ACCESS VBA:将 ISO 周数转换为日期范围时出错

[英]ACCESS VBA: Error when converting ISO Week Number to Date Range

I am trying to create a simple week selector based on the ISO Week number, which will give me the Monday date and the Sunday date every time the user clicks on "Current Week" or "Previous Week" or "Next Week" Buttons, as I will select all the transactions within those dates.我正在尝试根据 ISO 周数创建一个简单的周选择器,每次用户单击“当前周”或“上一周”或“下周”按钮时,它都会为我提供星期一日期和星期日日期,如我将选择这些日期内的所有交易。

I have managed doing that following the steps from MS Access get ISO standard week number to get the correct week number for a specific date, and then converting the week number back to date following https://answers.microsoft.com/en-us/msoffice/forum/msoffice_access-mso_other/convert-week-number-to-date/3d0f8c90-a155-e011-8dfc-68b599b31bf5 .我已经按照MS Access 中的步骤获取 ISO 标准周数以获取特定日期的正确周数,然后按照https://answers.microsoft.com/en-us将周数转换回日期/msoffice/forum/msoffice_access-mso_other/convert-week-number-to-date/3d0f8c90-a155-e011-8dfc-68b599b31bf5

My conversion works fine for this year, every time I click in previous or next week, it brings the correct Monday and Sunday along with its correct week number, however, when it arrives on week 1 of 2021, which brings the correct dates of 04/01/2021 and 10/01/2021 (from and to respectively), the next click on "next week" brings the dates "from = 06/01/2021" and "to = 12/01/2021", and it stops going forward, the clicks don't change the dates.今年我的转换效果很好,每次我在上一周或下周点击时,它都会带来正确的星期一和星期日以及正确的周数,但是,当它在 2021 年的第 1 周到达时,它会带来正确的日期 04 /01/2021 和 10/01/2021(分别为 from 和 to),下次单击“下周”会带来日期“from = 06/01/2021”和“to = 12/01/2021”,并且停止前进,点击不会改变日期。

When clicking "Previous Week", it goes well till week 1 of 2020, which brings the correct dates of 30/12/2019 and 05/01/2020, but the next click on "Previous Week" brings the dates 23/12/2018 and 29/12/18, but in this case, if I continue to click in Previous Week button it continues going back into 2018 correctly.当点击“上周”时,它一直持续到 2020 年的第 1 周,它带来了正确的日期 30/12/2019 和 05/01/2020,但接下来点击“上周”带来了日期 23/12/ 2018 年和 29/12/18,但在这种情况下,如果我继续单击“上一周”按钮,它将继续正确返回 2018 年。 It is just mad how it occurs.它是如何发生的,真是太疯狂了。

I believe that the problem is in the DateSerial when converting the Week Number to Date Range, I have tried to figure it out, but I couldn't do it.我相信问题出在将周数转换为日期范围时的 DateSerial,我试图弄清楚,但我做不到。

I hope you guys can help me out.我希望你们能帮助我。

Thank you in advance.先感谢您。

'''' This is the function in a module to get the week number

Public Function ISOWeek(MyDate As Date) As Integer

    ISOWeek = Format(MyDate, "ww", vbMonday, vbFirstFourDays)
    
    If ISOWeek > 52 Then
    
        If Format(MyDate + 7, "ww", vbMonday, vbFirstFourDays) = 2 Then ISOWeek = 1
        
    End If

End Function


'''' These subs run on the form code

Private Sub NextWeek_Click()

    Dim SelectedWeek As Date

    SelectedWeek = Me.Date_From.Value

    FirstDayWeek = DateAdd("ww", ISOWeek(SelectedWeek), DateSerial(Year(SelectedWeek), 1, 1) - 2)

    LastDayWeek = DateAdd("ww", ISOWeek(SelectedWeek), DateSerial(Year(SelectedWeek), 1, 1) + 4)

    Me.Date_From.Value = FirstDayWeek
    Me.Date_To.Value = LastDayWeek

End Sub

Private Sub PreviousWeek_Click()

    Dim SelectedWeek As Date

    SelectedWeek = Me.Date_From.Value
    
    FirstDayWeek = DateAdd("ww", ISOWeek(SelectedWeek) - 2, DateSerial(Year(SelectedWeek), 1, 1) - 2)

    LastDayWeek = DateAdd("ww", ISOWeek(SelectedWeek) - 2, DateSerial(Year(SelectedWeek), 1, 1) + 4)

    Me.Date_From.Value = FirstDayWeek
    Me.Date_To.Value = LastDayWeek

End Sub

Leave the week numbers from the date calculations, they only complicate matters.保留日期计算中的周数,它们只会使问题复杂化。

By using the generic functions listed below, your two functions can be reduced to:通过使用下面列出的通用函数,您的两个函数可以简化为:

Private Sub NextWeek_Click()

    Me.Date_From.Value = DateNextWeekPrimo(Me.Date_From.Value, vbMonday)
    Me.Date_To.Value = DateNextWeekUltimo(Me.Date_From.Value, vbMonday)

End Sub

Private Sub PreviousWeek_Click()

    Me.Date_From.Value = DatePreviousWeekPrimo(Me.Date_From.Value, vbMonday)
    Me.Date_To.Value = DatePreviousWeekUltimo(Me.Date_From.Value, vbMonday)

End Sub


' Returns the primo date of the week following the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateNextWeekPrimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = 1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)
    
    ' Return first weekday with no time part.
    ResultDate = DateAdd("d", 1 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DateNextWeekPrimo = ResultDate
    
End Function


' Returns the ultimo date of the week following the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateNextWeekUltimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = 1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)

    ' Return last weekday with no time part.
    ResultDate = DateAdd("d", 7 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DateNextWeekUltimo = ResultDate
    
End Function


' Returns the primo date of the week preceding the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DatePreviousWeekPrimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = -1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)
    
    ' Return first weekday with no time part.
    ResultDate = DateAdd("d", 1 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DatePreviousWeekPrimo = ResultDate
    
End Function


' Returns the ultimo date of the week preceding the week of the date passed.
'
' 2016-01-13. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DatePreviousWeekUltimo( _
    ByVal DateThisWeek As Date, _
    Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbSunday) _
    As Date

    Dim Interval    As String
    Dim Number      As Double
    Dim ResultDate  As Date
    
    Number = -1
    Interval = "ww"
    
    ' Offset date.
    ResultDate = DateAdd(Interval, Number, DateThisWeek)

    ' Return last weekday with no time part.
    ResultDate = DateAdd("d", 7 - Weekday(ResultDate, FirstDayOfWeek), Fix(ResultDate))
    
    DatePreviousWeekUltimo = ResultDate
    
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM