将访问日期设置为每月的各个工作日
Setting Access dates to various weekdays of the month
我在 Internet 上做了很多研究,想找到可以帮助我完成我正在尝试做的事情的东西,但一直没能想出任何对我的情况有帮助的东西。
我刚开始使用 MS Access(使用 2013 版),我正在通过我的 SharePoint 2013 网站使用数据库。我拥有的是一个 SharePoint 网站,它通过多个列表跟踪大量事件。本质上,我想做的是在 SharePoint 中创建一个列表模板,我可以将其上传到网站,而不是每月从头开始重新创建列表。有了这个列表,我每个月有 1800 个事件,我试图将 "Date" 字段设置为第一、第二、第三等。每个月的星期一,我也试图在星期二到星期五这样做,这样这样我就不必手动设置 1800 个日期了。
我认为这可以通过 MS Access 而不是通过 SharePoint 本身来实现。起初我以为我可以将默认值设置为我需要的每个日期字段,但据我所知这是行不通的,所以我想弄清楚是否有办法在SQL。现在请记住,我以前从未使用过 SQL,所以我真的不知道我在用它做什么。
因此,如果我没有解释我想要做好的事情,我会尝试将 Access 中的日期字段设置为每月的第三个星期三、每月的第二个星期四等。
感谢任何帮助!
谢谢!
看看 WEEKDAY
函数。
例如,您可以通过以下方式获取任何给定日期的下周日:
=[date] - WEEKDAY([date]) + 7
转换为纯 SQL 需要做一些工作,因为一个月中工作日的计数将是 4 或 5,但这里有一个使用 VBA 的通用函数这个:
' Calculates the date of the occurrence of Weekday in the month of DateInMonth.
'
' If Occurrence is 0 or negative, the first occurrence of Weekday in the month is assumed.
' If Occurrence is 5 or larger, the last occurrence of Weekday in the month is assumed.
'
' If Weekday is invalid or not specified, the weekday of DateInMonth is used.
'
' 2016-06-09. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateWeekdayInMonth( _
ByVal DateInMonth As Date, _
Optional ByVal Occurrence As Integer, _
Optional ByVal Weekday As VbDayOfWeek = -1) _
As Date
Const DaysInWeek As Integer = 7
Dim Offset As Integer
Dim Month As Integer
Dim Year As Integer
Dim ResultDate As Date
' Validate Weekday.
Select Case Weekday
Case _
vbMonday, _
vbTuesday, _
vbWednesday, _
vbThursday, _
vbFriday, _
vbSaturday, _
vbSunday
Case Else
' Zero, none or invalid value for VbDayOfWeek.
Weekday = VBA.Weekday(DateInMonth)
End Select
' Validate Occurence.
If Occurrence <= 0 Then
Occurrence = 1
ElseIf Occurrence > 5 Then
Occurrence = 5
End If
' Start date.
Month = VBA.Month(DateInMonth)
Year = VBA.Year(DateInMonth)
ResultDate = DateSerial(Year, Month, 1)
' Find offset of Weekday from first day of month.
Offset = DaysInWeek * (Occurrence - 1) + (Weekday - VBA.Weekday(ResultDate) + DaysInWeek) Mod DaysInWeek
' Calculate result date.
ResultDate = DateAdd("d", Offset, ResultDate)
If Occurrence = 5 Then
' The latest occurrency of Weekday is requested.
' Check if there really is a fifth occurrence of Weekday in this month.
If VBA.Month(ResultDate) <> Month Then
' There are only four occurrencies of Weekday in this month.
' Return the fourth as the latest.
ResultDate = DateAdd("d", -DaysInWeek, ResultDate)
End If
End If
DateWeekdayInMonth = ResultDate
End Function
我在 Internet 上做了很多研究,想找到可以帮助我完成我正在尝试做的事情的东西,但一直没能想出任何对我的情况有帮助的东西。
我刚开始使用 MS Access(使用 2013 版),我正在通过我的 SharePoint 2013 网站使用数据库。我拥有的是一个 SharePoint 网站,它通过多个列表跟踪大量事件。本质上,我想做的是在 SharePoint 中创建一个列表模板,我可以将其上传到网站,而不是每月从头开始重新创建列表。有了这个列表,我每个月有 1800 个事件,我试图将 "Date" 字段设置为第一、第二、第三等。每个月的星期一,我也试图在星期二到星期五这样做,这样这样我就不必手动设置 1800 个日期了。
我认为这可以通过 MS Access 而不是通过 SharePoint 本身来实现。起初我以为我可以将默认值设置为我需要的每个日期字段,但据我所知这是行不通的,所以我想弄清楚是否有办法在SQL。现在请记住,我以前从未使用过 SQL,所以我真的不知道我在用它做什么。
因此,如果我没有解释我想要做好的事情,我会尝试将 Access 中的日期字段设置为每月的第三个星期三、每月的第二个星期四等。
感谢任何帮助! 谢谢!
看看 WEEKDAY
函数。
例如,您可以通过以下方式获取任何给定日期的下周日:
=[date] - WEEKDAY([date]) + 7
转换为纯 SQL 需要做一些工作,因为一个月中工作日的计数将是 4 或 5,但这里有一个使用 VBA 的通用函数这个:
' Calculates the date of the occurrence of Weekday in the month of DateInMonth.
'
' If Occurrence is 0 or negative, the first occurrence of Weekday in the month is assumed.
' If Occurrence is 5 or larger, the last occurrence of Weekday in the month is assumed.
'
' If Weekday is invalid or not specified, the weekday of DateInMonth is used.
'
' 2016-06-09. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateWeekdayInMonth( _
ByVal DateInMonth As Date, _
Optional ByVal Occurrence As Integer, _
Optional ByVal Weekday As VbDayOfWeek = -1) _
As Date
Const DaysInWeek As Integer = 7
Dim Offset As Integer
Dim Month As Integer
Dim Year As Integer
Dim ResultDate As Date
' Validate Weekday.
Select Case Weekday
Case _
vbMonday, _
vbTuesday, _
vbWednesday, _
vbThursday, _
vbFriday, _
vbSaturday, _
vbSunday
Case Else
' Zero, none or invalid value for VbDayOfWeek.
Weekday = VBA.Weekday(DateInMonth)
End Select
' Validate Occurence.
If Occurrence <= 0 Then
Occurrence = 1
ElseIf Occurrence > 5 Then
Occurrence = 5
End If
' Start date.
Month = VBA.Month(DateInMonth)
Year = VBA.Year(DateInMonth)
ResultDate = DateSerial(Year, Month, 1)
' Find offset of Weekday from first day of month.
Offset = DaysInWeek * (Occurrence - 1) + (Weekday - VBA.Weekday(ResultDate) + DaysInWeek) Mod DaysInWeek
' Calculate result date.
ResultDate = DateAdd("d", Offset, ResultDate)
If Occurrence = 5 Then
' The latest occurrency of Weekday is requested.
' Check if there really is a fifth occurrence of Weekday in this month.
If VBA.Month(ResultDate) <> Month Then
' There are only four occurrencies of Weekday in this month.
' Return the fourth as the latest.
ResultDate = DateAdd("d", -DaysInWeek, ResultDate)
End If
End If
DateWeekdayInMonth = ResultDate
End Function