在VBA中的第一次循环中,检查始终为真

时间:2013-11-08 09:55:02

标签: vba ms-access

第一次循环后,“Check”以某种方式始终为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

1 个答案:

答案 0 :(得分: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