Cylcing through recordset with DAO isn't working

Hello

I have a Microsoft Access database with the following code attached to a button on a form:

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
   
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM tblEmployeesAwards WHERE EmailFlag=True")

        If Not rs.EOF Then
            rs.MoveFirst
            Do While Not rs.EOF
   
                MsgBox rs.Fields("EmployeeName") & "," & rs.Fields("AwardName")
       
                rs.MoveNext
           
            Loop
        End If

When I click the button, only the first matching record from the table is shown. However, when I copy this exact code into a module and run the sub, it works fine - it displays all matching records.

Any idea what I'm doing wrong? Is there something extra I have to do when the code is attached to a form? (I've used this same technique dozens of times before without a problem, but this time it's giving me grief.)

Thanks!
jrmcanada2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
try this revision for testing to find out how many records are returned by rs

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
   
    Set db = CurrentDb
    Set rs = db.OpenRecordset("SELECT * FROM tblEmployeesAwards WHERE EmailFlag=True")

        If Not rs.EOF Then
            rs.movelast
            msgbox "There are " & rs.recordcount & " records returned"

            rs.MoveFirst
            Do While Not rs.EOF
   
                MsgBox rs.Fields("EmployeeName") & "," & rs.Fields("AwardName")
       
                rs.MoveNext
           
            Loop
        End If
0
jrmcanada2Author Commented:
When I add those two lines to the code on the form, it tells me that only 1 record is returned. But when I add the same two lines to the code in the module, it tells me that 2 records are returned.

(The sub in the module is called TestLoop and I'm executing it by typing TestLoop in immediate mode.)
0
jrmcanada2Author Commented:
Could that problem be that the table is already open due to the form?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rey Obrero (Capricorn1)Commented:
what is the form record source?
0
Jeffrey CoachmanMIS LiasonCommented:
just for fun,
Try it like this:

Dim db As DAO.Database
Dim rs As DAO.Recordset
   
Set db = CurrentDb()
Set rs = db.OpenRecordset("SELECT * FROM tblEmployeesAwards WHERE EmailFlag=True")
    
    'Toggle the recordset to get an accurate count of the records
    rs.MoveFirst
    rs.moveLast
    msgbox "There are " & rs.recordcount & " records returned"
    
    'Loop the records
    rs.MoveFirst
    Do While Not rs.EOF
        MsgBox rs.Fields("EmployeeName") & "," & rs.Fields("AwardName")
        rs.MoveNext
    Loop
    
    msgbox "Done."

Open in new window

0
jrmcanada2Author Commented:
HI,

Thanks for your help. I found the problem. It turns out it was unrelated to DAO. I had a continuous form with checkboxes on each record and an email button in the header. The user needed to check the boxes for the records they wanted to email and then click the email button. I needed to add a line to save the current record when the email button was pressed because otherwise, the user would check the checkbox but it wouldn't get updated in the underlying record so the email routine wouldn't mail it.

Sorry for taking up your time with a DAO question when the problem was a silly mistake elsewhere.

James
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jrmcanada2Author Commented:
I realized the problem was another error unrelated to the topic of this question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.