循环访问表列,直到使用vba检测到更改

时间:2017-09-25 16:37:54

标签: loops ms-access access-vba recordset

我有一张包含全年员工姓名,周期和地点的表格。类似于下面的布局。我将它用于日历。该表没有周末

Emp1 01/02/2017 Business1
...
Emp1 10/02/2017 Business1
Emp1 10/03/2017 Business1
Emp1 10/04/2017 Business1
Emp1 10/05/2017 Business1
Emp1 10/06/2017 Business1
Emp1 10/09/2017 Business2
Emp1 10/10/2017 Business2
Emp1 10/11/2017 Business2
Emp1 10/12/2017 Business2
Emp1 10/13/2017 Business2
...
Emp999 12/31/2017假期

我需要遍历记录并找到位置的第一个日期和最后日期。范围并不总是5天。 我想最终得到像这三个领域的东西:
Emp1 | 10/02 / 2017-10 / 06/2017 |业务1
Emp1 | 10/09 / 2017-10 / 13/2017 |业务2

我知道我需要2个循环,但我无法弄清楚如何保持第一行值,同时继续循环以与下一行进行比较。

我尝试了直布罗陀的建议,看起来它可能有效,但每次输入只输出1天的范围。这是我用你的代码写的

Dim db As DAO.Database
Dim bolFirst As Boolean
Dim rst1 As DAO.Recordset
Dim strInstructor As String
Dim strDay As String
Dim strLocation As String
Dim strInstructorHold As String
Dim strDayHold As String
Dim strLocationHold As String
Dim strSql As String
Set db = CurrentDb

strSql = "SELECT tblTestData.RowId, tblTestData.Employee, tblTestData.WeekDate, tblTestData.Location FROM tblTestData ORDER BY tblTestData.WeekDate;"
    Set rst1 = db.OpenRecordset(strSql)
    bolFirst = True
    Do While Not rst1.EOF
        strInstructor = rst1!Employee
        strDay = rst1!WeekDate
        strLocation = rst1!Location
        If Not strInstructor = strInstructorHold Or Not strLocation = strLocationHold Or Not CDate(strDay) - 1 = holdDate Then
            If Not first Then
            Debug.Print strInstructorHold & ":" & strDayHold & "-" & holdDate & "-" & strLocationHold
            End If
            'Initial values
            strInstructorHold = rst1!Employee
            strDayHold = rst1!WeekDate
            strLocationHold = rst1!Location
       End If
    rst1.MoveNext
        holdDate = rst1!WeekDate
        bolFirst = False
    Loop

我没有看到你初始化holdDate变量的位置。

2 个答案:

答案 0 :(得分:1)

基本上,您需要创建三个变量来保存日期,当前,上一个,初始。然后,您需要两个用于保存Employee Current和Previous,然后两个用于Type Current和Previous。只要正确排序的数据集,您也不一定需要两个循环。

我在Excel中制作了一个模型,并试图使其足够通用,以便您理解:

Dim valCollection As Collection
Dim first As Boolean
Dim holdDate As Date
Dim firstColVal As String
Dim secondColVal As Date
Dim thirdColVal As String
Dim holdFirstColVal As String
Dim holdSecondColVal As Date
Dim holdThirdColVal As String

first = True
While Not empRS.EOF

    'Current Values
    firstColVal = empRS!employee
    secondColVal = empRS!date
    thirdColVal = empRS!type

    'If we are not the same employee or record type
    'or the date isn't consecutive with the last date
    If Not firstColVal = holdFirstColVal Or Not thirdColVal = holdThirdColVal Or _
        Not CDate(secondColVal) - 1 = holdDate _
    Then

        'Stop the first row from adding since it is a population stage
        If Not first Then
            'adding as pipe delimited
            'you can change delimiter or do something else with the data here
            valCollection.Add holdFirstColVal + "|" + _
                Format(holdSecondColVal, "MM/dd/yyyy") + "-" + Format(holdDate, "MM/dd/yyyy") + _
                "|" + holdThirdColVal

        End If

        'Initial Values
        holdFirstColVal = empRS!employee
        holdSecondColVal = empRS!date
        holdThirdColVal = empRS!type

    End If

    'Previous date
    holdDate = empRS!date
    first = False

Loop

'Add last item since the very last row will not get added to the collection otherwise
'Probably depends on scenario so you may not need it
valCollection.Add holdFirstColVal + "|" + Format(holdSecondColVal, "MM/dd/yyyy") + "-" + Format(holdDate, "MM/dd/yyyy") + "|" + holdThirdColVal

答案 1 :(得分:0)

帕特里克,我重新回答了你的答案,但我还必须添加一个我对查询的功能。

Function WeekNumber(ByVal vDate As Date) As Integer  
    'ISO week number Ref http://www.snb-vba.eu/VBA_ISO_weeknumber_en.html  
    WeekNumber = DatePart("ww", vDate - Weekday(vDate, 2) + 4, 2, 2)  
End Function

这让我也可以在组中包含周数,这给了我正在寻找的结果。