Count records in the recordset (1 Viewer)

dz2k7

Not only User
Local time
Yesterday, 23:01
Joined
Apr 19, 2007
Messages
104
How to count records in the recordset?

I tried

rc = rs.RecordCount

but it returns -1 all the time

Thanks
 

Kiwiman

Registered User
Local time
Today, 07:01
Joined
Apr 27, 2008
Messages
799
Howzit

after you have set the recordset try. I'm not sure if this is strictly necessary, but I have always done this.

rs.movelast
rs.movefirst
rc = rs.recordcount
 

Banana

split with a cherry atop.
Local time
Yesterday, 23:01
Joined
Sep 1, 2005
Messages
6,318
^

Indeed. With DAO, the recordset is ready to use as soon as first record or page is loaded, so you don't have accurate count unless you move to the last record. This is quite expensive.

But you get -1, which leads me to suspect that you are using ADO or querying a linked table. If this is the case, I believe you cannot retrieve recordcount with that method, and may have to use a Count(*) query to get the count. There may be workarounds that I don't know about as well.
 

Kiwiman

Registered User
Local time
Today, 07:01
Joined
Apr 27, 2008
Messages
799
Thanks for that Banana - at least I now know why moving to last is required.
 

chergh

blah
Local time
Today, 07:01
Joined
Jun 15, 2004
Messages
1,414
If your using ADO you will need to change the cursor type so where you open your recordset it would be something like:

Code:
rs.open "SELECT * FROM TABLE", connection, adOpenStatic

rs.recordcount should return the correct recordcount with adOpenStatic specified.
 

dz2k7

Not only User
Local time
Yesterday, 23:01
Joined
Apr 19, 2007
Messages
104
Chergh you won!

That works fine.

Thanks a lot!
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 07:01
Joined
Sep 12, 2006
Messages
15,701
if you have thie query as a stored query, which is generally more efficient anyway you can just do

dcount("*","myquery")
 

dz2k7

Not only User
Local time
Yesterday, 23:01
Joined
Apr 19, 2007
Messages
104
Cool!
That works too!
You guys are the best!
 

Users who are viewing this thread

Top Bottom