Solved

VBA Access recordset counting only 1

Posted on 2010-11-30
3
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

623 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