使用vba循环访问Access中所有记录的代码

时间:2014-03-06 21:10:29

标签: ms-access access-vba

我需要查看表格中的每条记录以及是否p<> 0弹出警告,记录数量不好,即时通讯使用此代码,但我没有错误循环

Private Sub Command142_Click()
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("ppp-pd")
Do While rs.EOF = True
rs.MoveFirst
p1 = (Round(rs!brutoprihod - (rs!brutoprihod * rs!ProcPrizTros) - rs!osnovicazaporez))
If p1 = 0 Then
rs.MoveNext
Loop
Else
MsgBox ("not good")
End If
MsgBox ("all records are good")
End Sub

1 个答案:

答案 0 :(得分:2)

你不能让If语句跨越循环。此外,我认为如果记录集为空,您的代码将只进入循环。试试这个:

Private Sub Command142_Click()

    Dim rs As Recordset

    Set rs = CurrentDb.OpenRecordset("ppp-pd")

    Do While Not rs.EOF
        p1 = (Round(rs!brutoprihod - (rs!brutoprihod * rs!ProcPrizTros) - rs!osnovicazaporez))
        If p1 <> 0 Then
            MsgBox ("not good")
        End If
        rs.MoveNext
    Loop
End Sub