Solved

VBA Access recordset counting only 1

Posted on 2010-11-30
3
430 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

685 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