Solved

Recordset.RecordCount in ADO

Posted on 2004-10-07
6
3,808 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now