1

我有一个现有的表,其中包含 1000 条记录。我需要根据客户 ID 和与之关联的日期字段更新每条记录。

基本上,我可以根据日期的顺序在每个日期旁边放一个有序的数字。

我想我需要使用两个“for each”循环来完成这项工作。IE

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

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

谢谢

4

2 回答 2

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
于 2013-03-11T11:14:51.657 回答
0

使用Recordset循环记录

于 2013-03-11T11:06:22.683 回答