上一年的周数

时间:2017-02-03 12:03:29

标签: sql vba date ms-access

我是一名访问护士,并为我的访问安排了Access 2016数据库。一份报告使用周数将“本周到期的人”分组给我看。目前,一些患者已经过期,他们的“下次到期”是2016年的日期,他们的“周数”是2016年的51或52或53 .... uggh!

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

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

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

1 个答案:

答案 0 :(得分:0)

此功能将根据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]))

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