UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Recordcount Returns 1 In Query With 300+ Records    
 
   
n8_Mills
post Mar 2 2012, 08:04 PM
Post #1

UtterAccess Enthusiast
Posts: 55
From: Renton, WA



Hey all,

This has me pretty mad because it's so simple and I can't make any sense of it. I have this:

CODE
    Dim db As DAO.Database
    Dim rstTask As DAO.Recordset
    
    Set rstTask = db.OpenRecordset("SELECT thisnQry.[Tracking Number] FROM thisnQry;")
    t1 = rstTask.RecordCount

If I double-click on the query "thisnQuery" it return 319 records, but t1 = 1 when I run this code.

It's the same if I run the code for the table that thisnQuery looks at. What could be happening?

Previous troubleshooting steps:
Simplified the query to the most basic "Select" statement
Set rstTask = db.OpenRecordset("SELECT thisnQry.* FROM thisnQry;")
Added Dim db As DAO.Database and Dim rstTask As DAO.Recordset
Changed references from Microsoft Office 12.0 Access Database Engine Object Library to Microsoft DAO 3.6 Object Library
Compiled / Decompiled

None of these steps made any difference.

Thanks,

Nate

This post has been edited by n8_Mills: Mar 2 2012, 08:04 PM
Go to the top of the page
 
+
DanielPineault
post Mar 2 2012, 08:24 PM
Post #2

UtterAccess VIP
Posts: 1,451



This is a know issue, you have to move to the last record before getting an accurate count, so try:
CODE
Dim db As DAO.Database
Dim rstTask As DAO.Recordset

Set db = CurrentDb    
Set rstTask = db.OpenRecordset("SELECT thisnQry.[Tracking Number] FROM thisnQry;")
With rstTask
    .MoveLast
    t1 = .RecordCount
End with
Go to the top of the page
 
+
jleach
post Mar 2 2012, 08:40 PM
Post #3

UtterAccess Editor
Posts: 6,709
From: Capital District, NY, USA



<picky>

I wouldn't call it an issue, per se, but rather a feature. This was done by design, for performance reasons. ADO recordsets do something similar, if the syntax/returns are not exactly the same.

The rs is designed to not load all records up front. This keeps performance good in large tables, as the records are only loaded for a certain amount past the cursor position instead of having to gather 100k records at once, for instance. A nonzero recordcount indicates only that there is in fact records, (with DAO, I believe this is a count of the number of records that have been loaded so far, but I'm not positive on that without looking it up)

</picky>

cheers,
Go to the top of the page
 
+
DanielPineault
post Mar 2 2012, 08:52 PM
Post #4

UtterAccess VIP
Posts: 1,451



Issue/feature, all in the eyes of the beholder! (IMG:style_emoticons/default/sarcasm.gif)

Valid point, and one I was not aware of. I learnt, yet again, something new by being at UA!
Go to the top of the page
 
+
jleach
post Mar 2 2012, 08:59 PM
Post #5

UtterAccess Editor
Posts: 6,709
From: Capital District, NY, USA



A little more info:

Quoting Walter from a different thread:

QUOTE
Certain types of ADO recordsets return -1 for the recordcount.

ForwardOnly and Dynamic return -1
Static and Keyset return real values


Quoting Brent from a different thread:

QUOTE
No matter the size, unless the DAO recordset is of .Type dbOpenTable, you will only get the first record. ADO recordsets have a nature to fully populate on load. That is why DAO is claimed to load faster -- it only loads one record! ADO is claimed (by me) to navigate faster -- well, the record is already loaded, so its easy to navigate to a record that is already in memory!


Cheers,
Go to the top of the page
 
+
gemmathehusky
post Mar 4 2012, 12:06 PM
Post #6

UtterAccess VIP
Posts: 1,875
From: UK



if i need a record count, i tend to do

dcount("*",somequery"), rather than manipulate the recordset cursors, to be honest
Go to the top of the page
 
+
n8_Mills
post Mar 5 2012, 01:10 PM
Post #7

UtterAccess Enthusiast
Posts: 55
From: Renton, WA



Thanks all, learn something new every day. I guess the "Recordcount" naming is misleading, it really should be RecordNum or something like that.
Go to the top of the page
 
+
Roger_Carlson
post Mar 5 2012, 01:16 PM
Post #8

UtterAccess VIP
Posts: 2,331
From: West Michigan



Some time ago, I posted the following to my blog, which might be of some interest:

What is the fastest way to return the Record Count from an Access Query?

Edited to Add:

BTW, you do not need to move to the last record if you are opening a TableType recordset. For a local table, you can do this:

CODE
lntCount = CurrentDb.OpenRecordset("Mytable").RecordCount
For a linked table, you can do this:

CODE
lntCount = OpenDatabase("c:\My Documents\LinkedDatabase.mdb").OpenRecordset("Mytable").RecordCount


which you can find here:
What is the fastest way to return the Record Count of a table?
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 18th May 2013 - 10:07 PM