0

第一次循环后,“检查”总是为 0,我一直在调试,但仍然找不到原因。任何想法?数据假设有时使“检查”为 0,但并非总是如此。

Private Sub Command12_Click()
    Dim db As Database
    Dim rs As DAO.Recordset
    Dim rs2 As DAO.Recordset
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Amity")
    Set rs2 = db.OpenRecordset("Opportunity")
    Set rs3 = db.OpenRecordset("SalesForceDonor")
    Set rs4 = db.OpenRecordset("Donor")

    While Not rs.EOF
        check = 0

        While Not rs3.EOF
            If rs("Donor_Code") = rs3("Donor_Code") Then
                check = 1
            End If
            rs3.MoveNext
        Wend

        If check = 0 Then
            rs4.AddNew
            rs4![Donor_Code] = rs![Donor_Code]
            rs4.Update
        End If

        rs2.AddNew
        rs2![Donor_Code] = rs![Donor_Code]
        rs2![Donation_name] = rs![Donation_name]
        rs2.Update
        rs.MoveNext
    Wend
    rs3.Close
    rs4.Close
    rs2.Close
    rs.Close
End Sub
4

1 回答 1

1

我发现了一些必须纠正的问题,为 rs 的每条记录添加 rs3.MoveFirst:

Private Sub Command12_Click()
    Dim check
    Dim db As Database
    Dim rs As DAO.Recordset
    Dim rs2 As DAO.Recordset
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Amity")
    Set rs2 = db.OpenRecordset("Opportunity")
    Set rs3 = db.OpenRecordset("SalesForceDonor")
    Set rs4 = db.OpenRecordset("Donor")

    While Not rs.EOF
        check = 0

        rs3.MoveFirst ' <= here we move to the first record of rs3!!!

        Do While Not rs3.EOF
            If rs("Donor_Code") = rs3("Donor_Code") Then
                check = 1
                Exit Do
            End If
            rs3.MoveNext
        Loop

        If check = 0 Then
            rs4.AddNew
            rs4![Donor_Code] = rs![Donor_Code]
            rs4.Update
        End If

        rs2.AddNew
        rs2![Donor_Code] = rs![Donor_Code]
        rs2![Donation_name] = rs![Donation_name]
        rs2.Update
        rs.MoveNext
    Wend
    rs3.Close
    rs4.Close
    rs2.Close
    rs.Close
End Sub
于 2013-11-08T10:33:33.607 回答