Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA Access recordset counting only 1

Posted on 2010-11-30
3
Medium Priority
?
448 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 2000 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 58
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

916 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