VB- ADO Returns a recordcount of -1

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
eojhanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SethiCommented:
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
Éric MoreauSenior .Net ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eojhanAuthor Commented:
Why does aduseclient work and aduseserver not? Why is this the only way to get at the recordcount property?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.