ADO vs DAO... RecordCount

To ge number the of records in a table, I use:

Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("Tabl1", dbOpenSnapshot)
If rst.RecordCount > 0 Then
    rst.MoveLast    '<-- this takes a long time (over 3,000,000 records)
    msgbox rst.RecordCount
End If
rst.Close

With ADO, however, we can have:

Dim rst 'As ADO....
Set rst = ...
msgbox rst.RecordCount
rst.Close

FYI: Table1 is in the current database not in a backend.

Could you please complete above few lines and make it work using ADO.

Thank you.




LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BillPowellCommented:
Dim rst As New ADODB.Recordset
rst.Open "Select * From MyTable", CurrentProject.Connection, adOpenKeyset
MsgBox rst.RecordCount
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BillPowellCommented:
The key part being requesting either a adOpenStatic or adOpenKeyset.  The other options do not support the RecordCount property
0
Patrick MatthewsCommented:
Hi Mike,

If all you want is a record count, then why not just do this:

Dim rst As DAO.Recordset
Dim RecCount As Long
Set rst = CurrentDb.OpenRecordset("SELECT Count(*) AS RecCount FROM Tabl1", dbOpenSnapshot)
RecCount = rst!RecCount
rst.Close


Regards,

Patrick
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Leigh PurvisDatabase DeveloperCommented:
And, if memory serves, the only ones Access supports.
0
Leigh PurvisDatabase DeveloperCommented:
And if absolute current accuracy isn't vital then
Currentdb.TableDefs("Tabl1").RecordCount
0
dqmqCommented:
Or use the access function that does essentially that:

msgbox dcount("*","Tabl1")

You do have a PK index on that table, right?
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
BillPowell,
You method looks good.  I will try it shortly.

Patrick,
I suspect Count(*) AS RecCount with over 3,000,000 records might be a bit slugish.  I will try if the solution from Bill fails.

LPurvis,
Currentdb.TableDefs("Tabl1").RecordCount looks very good.  It is lighting fast.  However, some of these are queries.  With a query, I tried:

Currentdb.QueryDefs("Query1").RecordCount

Which producess an error (method or data member not found, highlighting RecordCount).  What is the correct syntax for query if possible at all?

Thanks,

Mike



0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
dqmq,

No, I don't have index.  dcount("*","Tabl1"), I guess, would take much longer than any other solution.

Thanks,

Mike
0
Leigh PurvisDatabase DeveloperCommented:
The recordcount is a property of the tabledef - hence it's not guaranteed to be bang up to date.
But it is also why it's lightning fast.

If all you wanted was the recordcount of a query - I'd likely look to Patrick's first.
An index should make things better yes - but a full table scan needn't be the end of the world - and you're not pulling back all of the records.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.