Solved

Number of records in an ADO resultset (VBA)

Posted on 2006-11-15
9
2,438 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 65

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 65

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 500 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CheckListBox usage 3 82
PHP preg_replace code convert to Delphi 14 82
firemonkey keyboard covers the controls 1 53
How to Get Images From Server using App Tethering 11 42
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

726 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