[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Number of records in an ADO resultset (VBA)

Posted on 2006-11-15
9
Medium Priority
?
2,519 Views
Last Modified: 2013-11-23
Can anyone help me determine a number of records in an ADO resultset? I know it is easy to do in VB, but for some reason in VBA I can't get it (using VBA for excel if that makes a difference). Thanks in advance.
0
Comment
Question by:MorDeRor
9 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 17948126
Normally it would be rs.RecordCount.  

Depending on your cursor type it may show -1 initially, which means you either have to use a new cursor type, or rs.MoveLast then rs.MoveFirst.

afaik there is no difference in how an ADO recordset behaves based on whether you are using VB or VBA.
0
 
LVL 3

Author Comment

by:MorDeRor
ID: 17948244
right, it does show the value of -1, and it does not like movelast command either (I'm guessing it has to do with a specific type of cursor). In any case, it would be very clumsy to have to do

rs.movelast
msgbox rs.recordcount
rs.movefirst

isn't there a better way to get this info?
0
 
LVL 3

Author Comment

by:MorDeRor
ID: 17948329
OK, I think, the problem is solved. see this article for explanation:

http://www.itjungle.com/mgo/mgo121703-story01.html

The way I understood it, the idea is to request client side processing for the cursor, this way all the information about it is available right away, and rs.recordcount returns a valid value.
0
Technology Partners: 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!

 
LVL 66

Expert Comment

by:Jim Horn
ID: 17948352
If the performance hit for client-side processing is minimal, as the method you discovered gives you the rs.RecordCount right away, then go for it.
0
 
LVL 3

Author Comment

by:MorDeRor
ID: 17948369
yes, I am not so concerned about the performance hit on the client side, and I would like to right as little code as possible to get the number of records.
0
 

Expert Comment

by:DavidNL
ID: 17958370
I would do it like this:

Dim intRowCount as integer
rs.open "Select count (SomeTable.SomeColumnName) as MyCount from SomeTable")
intRowCount = rs!MyCount
rs.close

msgbox "Rowcount = " & intRowCount

Regards,
David.
0
 
LVL 3

Author Comment

by:MorDeRor
ID: 17958414
thanks. :) but would mean an extra trip to the server just to get the row count. Even though client side processing has it's limitations, I think it is still better than having to make a special call to the server. Especially if you consider that on an active system the resultset may have a different number of rows from what your query returns...
0
 
LVL 3

Accepted Solution

by:
Stimphy earned 2000 total points
ID: 18059025
I would assume that you can set the open method and the lock type...

try doing

adoRec.open (SQL),connection,adOpenKeyset adLockPessimistic

msgBox(adoRec.RecordCount)

0
 
LVL 3

Author Comment

by:MorDeRor
ID: 18061529
that was so much easier. Thank you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

831 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