Solved

VBA Access recordset counting only 1

Posted on 2010-11-30
3
398 Views
Last Modified: 2012-05-10
Hello

I've got a problem with a recordset variable that I am using for a loop. The code is as follows:

Set rst = dbs.OpenRecordset("SELECT * FROM LUL_Devices")
Counter = rst.RecordCount

While Counter <> 0....

...rst.MoveNext
Counter = Counter - 1

Although the table LUL_Devices has 5,625 records in it, rst.RecordCount is only returning 1, and so the loop is only updating one record. Can anyone advise why only 1 record is being stored in the recordset.

Many thanks in advance.
0
Comment
Question by:The_Hitcher
3 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34237507
Sometimes you have to go to the end before it will count properly

Set rst = dbs.OpenRecordset("SELECT * FROM LUL_Devices")
rst.MoveLast   ' try add this line
Counter = rst.RecordCount
0
 

Author Comment

by:The_Hitcher
ID: 34237831
That's done the trick. Thanks.
0
 
LVL 57
ID: 34238085
Yes, a movelast will make the recordset count accurate.

JET decides based on a number of factors how quickly it will populate a recordset.  When it first opens, the count will be 1 to indicate that there are records.  In somecases, the count will then be updated when JET is finished, but often it is not.

Instead of relying on recordcount, use BOF and EOF for loops.  This avoids doing the movelast, which is costly performance wise.

  Only do the movelast if you really need an accurate recordcount and event then, consider doing a SELECT COUNT(*) on the table, which is highly optimized.

JimD.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now