在访问中提取一系列日期并跳过假期

Posted

技术标签:

【中文标题】在访问中提取一系列日期并跳过假期【英文标题】:Pulling a range of dates in access and skipping holidays 【发布时间】:2017-01-10 21:01:07 【问题描述】:

我正在尝试使用 VBA 代码跳过假期。我已经编写了代码,并且我可以在工作日使用它,所以我只在周一到周五拉。该代码旨在提取前一天的总数,因此我总是落后 1 天,但我的问题是,当有假期时,代码会跳过假期,将其全部设为 0,我需要它做的就是否定这一天。

代码将逐步通过代码查找范围内有多少记录,到目前为止我整周收集了 7 条记录,但如果包含假期,我们可能会有 2 个假期将那些日子显示为记录的范围(我在数据库中输入空记录)。有没有办法我可以否定空值并让它跳过数据库中应该是工作日的空值,或者我可以创建一个子例程从一个有假期的表中提取并跳过那些仍然显示 5 个工作日的日子。

    Dim iWeekDay As Integer
 Dim sSQL As String

Dim objRecordset As ADODB.Recordset
Dim objRecordsetConn As ADODB.Connection
Dim objRecordsetComm As ADODB.Command

iWeekDay = WeekDay(Date)



Set objRecordsetConn = New ADODB.Connection
objRecordsetConn.ConnectionString = CurrentProject.Connection
objRecordsetConn.Open

'Start new section Collections

sSQL = "Select CollDate From tblTXandVACollDist Where CollDate Between date()-7 and date()-1 Group by CollDate Order by CollDate;"

Set objRecordsetComm = New ADODB.Command
    objRecordsetComm.ActiveConnection = objRecordsetConn
    objRecordsetComm.CommandText = sSQL
    objRecordsetComm.CommandType = adCmdText

    'Open ADO recordset
    Set objRecordset = New ADODB.Recordset
    objRecordset.ActiveConnection = objRecordsetConn
    Set objRecordset.Source = objRecordsetComm
    objRecordset.CursorLocation = adUseClient
    objRecordset.CursorType = adOpenStatic    'Allows both forward and backward movement in recordset
    objRecordset.LockType = adLockPessimistic 'Allows records to be edited.
    objRecordset.Open
    'objRecordset.MoveLast
    'objRecordset.MoveFirst

If objRecordset.RecordCount >= 1 Then
            dCollDate1 = objRecordset("CollDate")

        Else
            Select Case iWeekDay ' This is if there are no collection totals form the table the report will search for the current date and then use the below case to set the days so all other information that is pulled in will match with the collection date.
                Case 2   ' This is if the current day is monday
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 3
                Case 3 ' This is if the current day is Tue
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 1
                Case 4   ' This is if the current day is Wend
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 5    ' This is if the current day is Thur
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 6  ' This is if the current day is Fri
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 4
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
            End Select

        End If

        If objRecordset.RecordCount >= 2 Then
            objRecordset.MoveNext

            dCollDate2 = objRecordset("CollDate")

        Else
        Select Case iWeekDay ' This is if there are no collection totals form the table the report will search for the current date and then use the below case to set the days so all other information that is pulled in will match with the collection date.
                Case 2   ' This is if the current day is monday
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 3
                Case 3 ' This is if the current day is Tue
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 1
                Case 4   ' This is if the current day is Wend
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 5    ' This is if the current day is Thur
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 6  ' This is if the current day is Fri
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 4
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
            End Select

        End If

        If objRecordset.RecordCount >= 3 Then
            objRecordset.MoveNext


            dCollDate3 = objRecordset("CollDate")

        Else
            Select Case iWeekDay ' This is if there are no collection totals form the table the report will search for the current date and then use the below case to set the days so all other information that is pulled in will match with the collection date.
                Case 2   ' This is if the current day is monday
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 3
                Case 3 ' This is if the current day is Tue
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 1
                Case 4   ' This is if the current day is Wend
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 5    ' This is if the current day is Thur
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 6  ' This is if the current day is Fri
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 4
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
            End Select

        End If

        If objRecordset.RecordCount >= 4 Then
            objRecordset.MoveNext

            dCollDate4 = objRecordset("CollDate")


        Else

        Select Case iWeekDay ' This is if there are no collection totals form the table the report will search for the current date and then use the below case to set the days so all other information that is pulled in will match with the collection date.
                Case 2   ' This is if the current day is monday
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 3
                Case 3 ' This is if the current day is Tue
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 1
                Case 4   ' This is if the current day is Wend
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 5    ' This is if the current day is Thur
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 6  ' This is if the current day is Fri
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 4
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
            End Select

        End If

        If objRecordset.RecordCount = 5 Then 'this is a test portion to see if i have to adjust the chart to make sure the chart works on an individual basis per day
            objRecordset.MoveNext

            dCollDate5 = objRecordset("CollDate")


        Else

        Select Case iWeekDay ' This is if there are no collection totals form the table the report will search for the current date and then use the below case to set the days so all other information that is pulled in will match with the collection date.
                Case 2   ' This is if the current day is monday
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 3
                Case 3 ' This is if the current day is Tue
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 4
                    dCollDate5 = Date - 1
                Case 4   ' This is if the current day is Wend
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 5
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 5    ' This is if the current day is Thur
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 6
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
                Case 6  ' This is if the current day is Fri
                    dCollDate1 = Date - 7
                    dCollDate2 = Date - 4
                    dCollDate3 = Date - 3
                    dCollDate4 = Date - 2
                    dCollDate5 = Date - 1
            End Select


        End If

【问题讨论】:

【参考方案1】:

我最初的想法是你可以做这样的查询:

Select MAX(CollDate) FROM tblTXandVACollDist WHERE [otherfield] <> 0 AND (CollDate <> (select max(CollDate) FROM tblTXandVACollDist [otherfield] <> 0))  )

根据您使用的数据库,语法可能会有所不同,但这样的查询会告诉数据库为您提供今天 inst 的最后日期,其中您要比较的任何字段都不 = 0。

然后您可以更改它以提供 today-2、today-3 等,这应该会减少您必须在那里使用的许多逻辑。 (也可以使用这个查询,它可能更容易修改。取决于你的数据库)

Select MAX(CollDate) FROM tblTXandVACollDist WHERE [otherfield] <> 0 AND (CollDate <> (select TOP 1 CollDate FROM tblTXandVACollDist WHERE [otherfield] <> 0 ORDER BY CollDate))

并将其更改为最近两天的折扣

Select MAX(CollDate) FROM tblTXandVACollDist WHERE [otherfield] <> 0 AND (CollDate not IN(select TOP 2 CollDate FROM tblTXandVACollDist WHERE [otherfield] <> 0 ORDER BY CollDate))

【讨论】:

我正在使用 MS 访问【参考方案2】:

如果您有一个假期表,您应该能够提取工作日的计数,比如五个,通过一个简单的查询选择昨天和过去的日期,而不是周末,而不是假期:

Select 
    Top 5 *
From 
    tblTXandVACollDist 
Where 
    CollDate < Date()
    And
    Weekday([CollDate], 2) <= 5
    And
    CollDate Not In (Select HolidayDate From tblHolidays)
Order By
    CollDate Desc

【讨论】:

以上是关于在访问中提取一系列日期并跳过假期的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Angular/Javascript 中排除假期日期

如何在给定日期前 14 天避免假期

从日期计算中排除周末和自定义日期(即假期)

如何在 jQuery UI 日期选择器中禁用公共假期?

在Python中动态计算不包括假期日历的工作日数

十月一假期作业