Solved

Recordset.RecordCount in ADO

Posted on 2004-10-07
6
3,817 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 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
Technology Partners: 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

Technology Partners: 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!

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…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

734 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