迭代表并更新匹配的记录集 - Access - Vba

时间:2015-07-10 20:15:46

标签: sql vba ms-access access-vba ms-access-2003

问题

我构建了一个跟踪系统,其中包含来自其他表格的唯一workIDId。我有一个名为乘数的列,如果满足某些条件,我想改变。我想更改每个记录的乘数列,其中:日期是星期日,星期六之前(表示星期六的日期需要在表中)并且具有与星期六相同的ID。

基本上这是一个奖励系统,如果一个跑步者在周六和周日工作,那么在那个周日他得到2的乘数。我打开每个星期日和星期六在表中的记录集并尝试匹配它们。但它跳过一些东西,因为星期六并不总是跟着星期天。

代码

sqlFindSat = "Select WorkID, Date, Task, ID, Multiplier from tblA where  (weekday([date]) = 7) order by date, id, workId"
sqlFindSun = "select WorkID, Date, Task,  ID, Multiplier from tblA where ( weekday([date]) = 1 ) order by date, id, workId"


Set fwSun = db.OpenRecordset(sqlFindSun)
Set fwSat = db.OpenRecordset(sqlFindSat)

Do While Not fwSat.EOF
    waitforsat = False
    debug.print " Curr Sat: " & fwSat!Date & " Curr Sun: " & fwSun!Date

    If WeekdayName(Weekday(fwSat!Date)) = "saturday" Then
       If ((DateAdd("d", 1, fwSat!Date) = fwSun!Date) And (fwSun!ID = fwSat!ID)) Then
            fwSun.Edit
            fwSun!multiplier = 2
            fwSun.Update
       End If

    End If

    If Not fwSun.EOF Then
        fwSun.MoveNext
    End If

    fwSat.MoveNext
Loop

只要星期六之后是星期日,我的代码就可以了。一个星期六没有一个星期天,然后游标处于不同的水平,有时一个。我不介意在SQL Update或其他任何内容中执行此操作,只要它在访问中有效。

注意:下面的示例不是真实样本。评论部分用于显示一周中的哪一天。

样本表tblA

Workid |    Date     | ID | Multiplier | comments 

  12     10-11-2012    2       1           sunday

  13     15-12-2012     4      1           Monday

  14     10-10-2012    3       1           Saturday

  25     11-10-2012    3       1           Sunday

  17     8-10-2012     2       1           sunday

  35     15-10-2012     4       1           Monday

  45     8-10-2012     3       1           Saturday

  50     9-10-2012     3       1           Sunday    

在tblA上运行代码后的最终输出:

Workid |    Date     | ID | Multiplier | comments 

  12     10-11-2012    2       1           Sunday

  13     15-12-2012     4      1           Monday

  14     10-10-2012    3       1           Saturday

  25     11-10-2012    3       2           Sunday

  17     8-10-2012     2       1           sunday

  35     15-10-2012     4       1           Monday

  45     8-10-2012     3       1           Saturday

  50     9-10-2012     3       2           Sunday

从表中可以看出,之前的星期日。

2 个答案:

答案 0 :(得分:1)

VBA不需要这样做;您也可以使用自联接运行SQL查询并直接执行更新。

对于具有相同ID的前一个星期六的记录的所有星期日行,此查询会将乘数更改为2。

语法应为:

UPDATE tblA t1
INNER JOIN tblA t2 ON t1.ID = t2.ID AND t2.Date = DATEADD('d', -1, t1.date)
SET t1.Multiplier = 2
WHERE WEEKDAY(t1.date) = 1  -- assuming 1 is Sunday, might depend on regional setting

如果您想查看在执行更新之前会发生什么,只需将更新更改为select *并删除set语句。

或者在运行之前对表进行备份:)

答案 1 :(得分:0)

根据您要访问的记录数量,我认为这应该对您有用:

第一个功能就是遍历fwsat RecordSet中的所有星期日。对于每个星期日,它将调用PrecedingSaturday函数并检查该ID是否具有在该星期日的一天内的星期六。

 
Public Function CheckSunday()

    sqlFindSat = "Select WorkID, Date, Task, ID, Multiplier from tblA where  (weekday([date]) = 7) order by date, id, workId"
    sqlFindSun = "select WorkID, Date, Task,  ID, Multiplier from tblA where ( weekday([date]) = 1 ) order by date, id, workId"


    Set fwSun = db.OpenRecordset(sqlFindSun)
    Set fwsat = db.OpenRecordset(sqlFindSat)

    'For all Sundays
    Do While Not fwSun.EOF

        'If this Sunday has a Preceding Saturday. 
        If PrecedingSaturday(fwsat, fwSun!id, fwSun!Date) Then

            'Update multiplier to 2 for this Sunday
            fwSun.Edit
            fwSun!multiplier = 2
            fwSun.Update

        End If

        fwSun.MoveNext
    Loop

End Function

这个函数只是接受包含所有星期六的RecordSet,并搜索它们以查看它们中是否有任何星期六在有问题的星期日之前。如果是,则返回true。

Public Function PrecedingSaturday(fwsat As Recordset, intID As Integer, dte As Date) As Boolean

    Dim blnPrecedingSaturday As Boolean

    blnPrecedingSaturday = False

    'For all Saturdays while a preceding Saturday hasn't already been found
    Do While Not fwsat.EOF And blnPrecedingSaturday = False

        'If Saturday's ID and Sunday's id match
        If fwsat!id = intID Then
            'If the Saturday and Sunday happened within 1 day
            If DateDiff("d", fwsat!Date, dte) = 1 Then
                'This is a preceding Saturday
                blnPrecedingSaturday = True
            End If
        End If

        fwsat.MoveNext
    Loop

    PrecedingSaturday= blnPrecedingSaturday

End Function

这是粗略的代码,因为我没有表结构来正确测试它。虽然我认为它应该适用于您正在寻找的东西。