VB- ADO Returns a recordcount of -1

Posted on 2003-02-24
Medium Priority
Last Modified: 2010-05-01
This should be a really easy one. I have the following code:

SQLConnect = "Provider=SQLOLEDB;Data Source=CLUSTERNAME\CLUSTERINSTANCE;Initial Catalog=MyTable;Integrated Security=SSPI;"
Conn1.ConnectionString = SQLConnect
Cmd1.ActiveConnection = Conn1
Cmd1.CommandText = "Select MyColumn from MyTable where Need2Publish = 1"
Set Rs1 = Cmd1.Execute
i = 1
x = 1
intCount = Rs1.RecordCount
MsgBox intCount

This always produces a RecordCount of -1, even though there are 2 records? I know for a fact there are two records because if I do this:

intCount = 2
If intCount > 0 Then
    ReDim strVarName(intCount)
    Do Until Rs1.EOF
        strVarName(i) = Rs1.Fields(0).Value
        MsgBox (strVarName(i))
        i = i + 1
End If

It returns my 2 values in the message box...

Does anyone have an explanation to this?


Question by:eojhan
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
LVL 18

Expert Comment

ID: 8012910
This is becuase you are not specifying the type of recordset that you want to open. ADO by default will open a Forward only recordset and it will always return -1. Open a dynamic or keyset recordset and it will give you a recordcount. Another way of getting the total no of records is by opening another recordset:
strSQL="Select Count(*) from tblTableName"
LVL 70

Accepted Solution

Éric Moreau earned 200 total points
ID: 8013096
Do not use a Command object for a simple query like this. Conisder this:

SQLConnect = "Provider=SQLOLEDB;Data Source=CLUSTERNAME\CLUSTERINSTANCE;Initial Catalog=MyTable;Integrated Security=SSPI;"
Conn1.ConnectionString = SQLConnect
set rs1 = new adodb.recordset
rs1.cursorlocation = aduseclient
rs1.open "Select MyColumn from MyTable where Need2Publish = 1", conn1
msgbox Rs1.RecordCount


Author Comment

ID: 8017328
Why does aduseclient work and aduseserver not? Why is this the only way to get at the recordcount property?

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

764 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