在访问和跳过假期中拉出一系列日期

时间:2017-01-10 21:01:08

标签: mysql sql vba ms-access

我正在尝试使用VBA代码跳过假期。我已经编写了代码并且我在工作日使用它所以我只在星期一到星期五。该代码旨在拉动前一天的总数,所以我总是落后一天,但我的问题是,当有一个假期代码跳过假期,留下它所有0我需要它做的是否定所有的一起。

“守则”将逐步查看代码,以查找到目前为止整个星期我收集的范围内有多少记录,但如果包含假期,我们可以在它将那些天显示为记录(我在数据库中输入空记录)。有没有办法,我可以只是否定空值,并让它在工作日应该是的数据库中跳过空值,或者我可以创建一个子程序从一个有假期的表中拉出来,只是跳过那些仍然显示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

2 个答案:

答案 0 :(得分:0)

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

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

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

然后你可以改变这个给你今天2,今天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))

答案 1 :(得分:0)

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

<section>
          <div class="bg-hands row"><!--row div start-->
          <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
              <div class="col-lg-6 pull-right">
                 <img src="http://xiostorage.com/wp-content/uploads/2015/10/test.png" class="img-circle coral img-responsive" style="">
              </div>
             <div class="left col-lg-6 col-md-6 col-sm-6 col-xs-6" style="width: 100%">
                 <h2>lorem ipsem</h2>
                 <h4>doller sit amet</h4>
                 <hr>
                 <p id="p-left">Vivamus suscipit tortor eget felis.</p>
          </div>  
          </div>
          <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">

        <div class="col-lg-6 pull-left">
              <img src="http://xiostorage.com/wp-content/uploads/2015/10/test.png" class="img-circle steelblue img-responsive" style="">

          </div>
             <div class="right col-lg-6 col-md-6 col-sm-6 col-xs-6" style="width: 100%">
                 <h2>lorem ipsem</h2>
                 <h4>doller sit amet</h4>
                 <hr>
                 <p id="p-right">Vivamus suscipit tortor eget felis.</p>      
          </div> 
          </div>
          </div>
      </section>