Solved

Recordset.RecordCount in ADO

Posted on 2004-10-07
6
3,805 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
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 69

Accepted Solution

by:
Éric Moreau earned 20 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

708 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

12 Experts available now in Live!

Get 1:1 Help Now