Solved

Number of records in an ADO resultset (VBA)

Posted on 2006-11-15
9
2,423 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now