对于MS Access VBA中的多个客户ID的每个循环

时间:2013-03-11 11:00:57

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

我有一张存有1000条记录的现有表格。我需要根据客户ID和与之关联的日期字段更新每条记录。

基本上我可以在每个日期旁边放一个有序的数字,具体取决于日期的顺序。

我想我需要为每个循环使用两个'来完成这个。即。

For Each Customer ID in tblCustomers

'gather all records for that customer and get all dates in order from each record via recordset?

    For Each Date 
    newfield = newfield+ 1
    end loop

end loop

有人能指出我正确的方向来解决这个问题吗?

由于

2 个答案:

答案 0 :(得分:3)

如下所示:

Dim rstCustomers As DAO.Recordset
Set rstCustomers = CurrentDb.OpenRecordset("SELECT CustomerID FROM tblCustomers GROUP BY CustomerID")
If rstCustomers.RecordCount > 0 Then
    rstCustomers.MoveFirst
    Do Until rstCustomers.EOF
        Dim rstRecords As DAO.Recordset
        Set rstRecords = CurrentDb.OpenRecordset("SELECT RecordDate, OrderField FROM tblRecords WHERE CustomerID = " & rstCustomers!CustomerID & " ORDER BY RecordDate")
        If rstRecords.RecordCount > 0 Then
            Dim iCount as Integer
            iCount = 1
            rstRecords.MoveFirst
            Do Until rstRecords.EOF
                rstRecords.Edit
                   rstRecords!OrderField = iCount
                rstRecords.Update

                iCount = iCount + 1
                rstRecords.MoveNext
            Loop
        End If
        rstRecords.Close
        Set rstRecords = Nothing

        rstCustomers.MoveNext
    Loop
End If
rstCustomers.Close
Set rstCustomers = Nothing

答案 1 :(得分:1)

使用Recordset

循环记录