0

Here is my code. When it runs, count goes to 32,767. The procedure is only working with 5 records.

I tried MoveLast and MoveFirst together with no result.

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

The code should go through the five records and count the number of people in each age group. It does go through all five records, but endless times.

5
  • 1
    As it stands at this moment, this is not a complete code-sample - for example, there are references to variables that have no declarations. My suspicion is that your entire query process is being executed infinitely, but we can't see what's wrapped around it to judge conclusively. Commented Jun 30, 2019 at 19:51
  • 2
    While (Not peoplerec.EOF) should be While Not .EOF inside the With expression. Commented Jun 30, 2019 at 22:22
  • I didn't put the whole app because it is large and this is where the problem is. The only external variable is ziprec!household_id, which works correctly. This section gets an ID number and then gets 5 people in the family. The problem is when this loop keeps running instead of seeing an end of file after the 5 items. Commented Jun 30, 2019 at 22:35
  • @LeeMac, shouldn't make a difference that recordset variable is repeated - it's unnecessary but shouldn't hurt. What is ziprec - another recordset that is being looped? How many households are there? Why would there be five people in household? I also think there is more code we need to see. These calcs can probably be done in aggregate query or with DCount() in textboxes. Commented Jun 30, 2019 at 23:13
  • 1
    Debugging VBA Code -- step through your code. Commented Jul 1, 2019 at 5:09

1 Answer 1

1

Try to debug it like this (in immediate window Ctrl + G in VBA editor):

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

Because I'm not sure, that there is 5 records exactly

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.