【问题标题】:Weeknumber of prior year上一年的周数
【发布时间】:2017-06-20 19:16:05
【问题描述】:

我是一名访问护士,并且有一个 Access 2016 数据库来安排我的访问。一份报告使用周数将“本周到期的人”分组以供我查看。就目前而言,一些患者已经过期了,他们的“下一个到期日”是 2016 年的日期,使得他们的“周数”是 2016 年的 51 或 52 或 53 ......ugh!

分组中有没有办法在报告顶部设置这个 2016 日期,因为它和“旧”日期......2016......同时保持 2017 年的排序?也就是说,首先显示过期的 2016 年日期,然后显示 2017 年。

报告的来源是对表的查询。在这个查询中,我有: NV_wknum: DatePart("ww",([Last S-Visit]+60)) 作为该特定条目的周数(NV 是下次访问,即上次访问后 60 天)。

2016 年的日期显示在报告的末尾,因为周数是 51,等等...

【问题讨论】:

    标签: sql vba date ms-access


    【解决方案1】:

    此函数将根据 ISO 8601 标准返回任何给定日期的年份和星期数:

    Public Function ISO_WeekYearNumber( _
      ByVal datDate As Date, _
      Optional ByRef intYear As Integer, _
      Optional ByRef bytWeek As Byte) _
      As String
    
    ' Calculates and returns year and week number for date datDate according to the ISO 8601:1988 standard.
    ' Optionally returns numeric year and week.
    ' 1998-2007, Gustav Brock, Cactus Data ApS, CPH.
    ' May be freely used and distributed.
    
      Const cbytFirstWeekOfAnyYear  As Byte = 1
      Const cbytLastWeekOfLeapYear  As Byte = 53
      Const cbytMonthJanuary        As Byte = 1
      Const cbytMonthDecember       As Byte = 12
      Const cstrSeparatorYearWeek   As String = "W"
    
      Dim bytMonth                  As Byte
      Dim bytISOThursday            As Byte
      Dim datLastDayOfYear          As Date
    
      intYear = Year(datDate)
      bytMonth = Month(datDate)
      bytWeek = DatePart("ww", datDate, vbMonday, vbFirstFourDays)
    
      If bytWeek = cbytLastWeekOfLeapYear Then
        bytISOThursday = Weekday(vbThursday, vbMonday)
        datLastDayOfYear = DateSerial(intYear, cbytMonthDecember, 31)
        If Weekday(datLastDayOfYear, vbMonday) >= bytISOThursday Then
          ' OK, week count of 53 is caused by leap year.
        Else
          ' Correct for Access97/2000+ bug.
          bytWeek = cbytFirstWeekOfAnyYear
        End If
      End If
    
      ' Adjust year where week number belongs to next or previous year.
      If bytMonth = cbytMonthJanuary Then
        If bytWeek >= cbytLastWeekOfLeapYear - 1 Then
          ' This is an early date of January belonging to the last week of the previous year.
          intYear = intYear - 1
        End If
      ElseIf bytMonth = cbytMonthDecember Then
        If bytWeek = cbytFirstWeekOfAnyYear Then
          ' This is a late date of December belonging to the first week of the next year.
          intYear = intYear + 1
        End If
      End If
    
      ISO_WeekYearNumber = CStr(intYear) & cstrSeparatorYearWeek & Format(bytWeek, "00")
    
    End Function
    

    您可以使用不同的周系统。然后只是 - 在你的报告中 - 首先排序

    Year(DateAdd("d",60,[Last S-Visit]))
    

    然后开始

    DatePart("ww",DateAdd("d",60,[Last S-Visit]))
    

    如果 60 天意味着 2 个月,则添加月份:

    Year(DateAdd("m",2,[Last S-Visit]))
    

    然后:

    DatePart("ww",DateAdd("m",2,[Last S-Visit]))
    

    请记住从驱动报告的查询中删除所有排序;排序必须在报告本身中指定。

    【讨论】:

      猜你喜欢
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多