Solved

Number of records in an ADO resultset (VBA)

Posted on 2006-11-15
9
2,430 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

776 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