将访问日期设置为该月的各个工作日

时间:2017-09-11 18:47:22

标签: sql sharepoint-2013 ms-access-2013

我已经通过互联网进行了大量研究,以帮助我完成我想要做的事情,但却未能提出任何有助于我的情况的事情。

我是使用MS Access(使用2013版)的新手,我正在通过SharePoint 2013网站使用数据库。我所拥有的是一个通过多个列表跟踪大量事件的SharePoint站点。基本上我要做的是在SharePoint中创建一个列表模板,我可以上传到网站,而不是每个月从头开始重新创建列表。有了这个列表,我每月有1800个事件,我正在尝试将“日期”字段设置为第一个,第二个,第三个等等。本月的星期一,我也试图在周二到周五这样做,所以这样我不必手动设置1800个日期。

我认为这可以通过MS Access而不是通过SharePoint本身实现。起初我以为我能够将默认值设置为我需要的每个日期字段,但从我的理解,这将无法工作,所以我想弄清楚是否有办法在SQL。现在请记住,我之前从未使用过SQL,所以我真的不知道我在做什么。

所以,如果我没有解释我想要做什么,我试图将Access中的日期字段设置为本月的第3个星期三,本月的第二个星期四等等。

任何帮助表示赞赏! 谢谢!

2 个答案:

答案 0 :(得分:0)

查看WEEKDAY功能。

例如,您可以使用以下内容获取任何给定日期的下一个星期日:

=[date] - WEEKDAY([date]) + 7

答案 1 :(得分:0)

转换为纯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