Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB- ADO Returns a recordcount of -1

Posted on 2003-02-24
3
Medium Priority
?
178 Views
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
Conn1.Open
Cmd1.ActiveConnection = Conn1
Cmd1.CommandText = "Select MyColumn from MyTable where Need2Publish = 1"
Set Rs1 = Cmd1.Execute
Rs1.MoveFirst
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))
        Rs1.MoveNext
        i = i + 1
    Loop
End If

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

Does anyone have an explanation to this?

TIA,

Owen
0
Comment
Question by:eojhan
3 Comments
 
LVL 18

Expert Comment

by:Sethi
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"
rsGetData.Open
0
 
LVL 70

Accepted Solution

by:
É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
Conn1.Open
set rs1 = new adodb.recordset
rs1.cursorlocation = aduseclient
rs1.open "Select MyColumn from MyTable where Need2Publish = 1", conn1
msgbox Rs1.RecordCount

0
 

Author Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

580 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