Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Recordset.RecordCount in ADO

Posted on 2004-10-07
6
Medium Priority
?
3,820 Views
Last Modified: 2013-12-25
According to the following code, why always the result in MsgBox = -1

------------------------------------------------------
Dim conn As new Connection
Dim rs As New Recordset
      
conn.ConnectionString  "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog= db_2004;Data Source=MyPC"
conn.Open
rs.Open " SELECT * FROM Items ", conn, adOpenDynamic, adLockOptimistic

MsgBox rs.RecordCount
------------------------------------------------------

I's sure there are data and I get them using :
MsgBox rs(0).Value & " " & rs(1).Value

Could any one help please ... Thanks
0
Comment
Question by:programmer_bmb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12247410
rs(0).Value and & rs(1).Value are the values of some fileds of the current record

Maybe you are looking for this rs.Fields.Count?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 80 total points
ID: 12247657
set your CursorLocation to adUseClient
rs.CursorLocation = adUseClient
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 12247877
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdprorecordcount.asp

The cursor type of the Recordset object affects whether the number of records can be determined. The RecordCount property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and either -1 or the actual count for a dynamic cursor, depending on the data source. Also the cursorlocations has an influence on the count.

0
Independent Software Vendors: 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 16

Expert Comment

by:JohnBPrice
ID: 12261058
emoreau & Dhaest are right.  

The reason for this is ADO will not fetch all the records from your database if it does not need to, and so doesn't know the record count until it gets to the last row.  If your dataset is small (a few thousands of small rows) using static/client side cursors is fine.  If your dataset is very large, however, using static or client side cursors can be slow, as they fetch all the records, and then return the first one to you.  If you aren't necessarily going to use all the records, you can do
"select count(*) from Items" before hand to get the count, which is very fast in SQL Server because it maintains statistics so it doesn't have to count all the records each time ( as long as you have a primary key).
0
 

Author Comment

by:programmer_bmb
ID: 12265741
Dhaest , JohnBPrice .. Thanks a lot for this rich information
0
 

Author Comment

by:programmer_bmb
ID: 12410351
Is it same for:
myConn.Excexute (" SELECT * FROM Items " )
or there is different ?

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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 …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

688 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