尝试遍历五个记录记录集并获得无限循环

时间:2019-06-30 19:31:06

标签: ms-access access-vba

这是我的代码。当它运行时,计数为32,767。该过程仅处理5条记录。

我尝试了MoveLastMoveFirst,但没有结果。

strSQL = "SELECT * FROM People WHERE people.household_id=" & ziprec!household_id
Set peoplerec = CurrentDb.OpenRecordset(strSQL)
With peoplerec
    While (Not peoplerec.EOF)
        currentage = DateDiff("yyyy", !birthday, rightnow)
        Select Case currentage
            Case 0 To 4
                age_under5 = age_under5 + 1
            Case 5 To 17
                age_5to17 = age_5to17 + 1
            Case 18 To 64
                age_18to64 = age_18to64 + 1
            Case Else
                age_over65 = age_over65 + 1
        End Select
        count = count + 1
        .MoveNext
    Wend
End With

该代码应遍历五条记录,并对每个年龄段的人数进行计数。它确实经历了所有五项记录,但是无尽的时间。

1 个答案:

答案 0 :(得分:1)

尝试这样调试它(在VBA编辑器的即时窗口Ctrl + G中):

strSQL = "SELECT * FROM People WHERE people.household_id=" & ziprec!household_id
Set peoplerec = CurrentDb.OpenRecordset(strSQL)
Debug.Print "strSQL is: " & strSQL
peoplerec.MoveLast
Debug.Print "peoplerec recordcount is " & peoplerec.RecordCount
peoplerec.MoveFirst
With peoplerec
    While (Not peoplerec.EOF)
        currentage = DateDiff("yyyy", !birthday, rightnow)
        Select Case currentage
            Case 0 To 4
                age_under5 = age_under5 + 1
            Case 5 To 17
                age_5to17 = age_5to17 + 1
            Case 18 To 64
                age_18to64 = age_18to64 + 1
            Case Else
                age_over65 = age_over65 + 1
        End Select
        count = count + 1
        .MoveNext
    Wend
End With

因为我不确定,确切地有5条记录