adodb recordset returning -1 when using Trusted Connection

I've been using SQL authentication for years in my code and checking the recordcount of a select stored procedure would correctly return the number of records in the underlying query statement. I am now trying to use Windows authentication to connect to the database, but the recordcount property for the same code is returning -1.  How can I get it to return the correct recordcount?
Here is my original connection string that works:
Provider=MSDataShape.1;Persist Security Info=True;Data Source=LBAPP1;User ID=sa;Password=MyPassword;Initial Catalog=MyDatabase;Data Provider=SQLOLEDB.1
Here is new trusted connection string:
Provider=SQLOLEDB;Data Source=LBApp1;Initial Catalog=MyDatabase;Trusted_connection=yes;

Here is the code that I'm running:

Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
cn.ConnectionString = strDBConnection
cn.Open
cmd.ActiveConnection = cn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "qdfSiteLogin"
cmd.Parameters(1) = strLoginComputer
Set rs = cmd.Execute
If rs.RecordCount > 0 Then
  rs.MoveFirst
Mel BrooksCITOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Pawan KumarDatabase ExpertCommented:
Please try this --

Provider=sqloledb;Data Source=LBApp1;Initial Catalog=myDataBase;
Integrated Security=SSPI;
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
but the recordcount property for the same code is returning -1
could be a cursor issue.

if you just want to know if got record returned or not, you can use EOF for verification as well, like:

If rs.eof  = false Then
     //there are record...
end if

Open in new window

0
Mel BrooksCITOAuthor Commented:
Pawan.  Changing the connection string to what you suggested still returns a -1 on the recordcount property

Ryan. Trying your solution produced the following error when going to my next line of code.
    "Rowset position cannot be restarted."

that next line of code being:   rs.MoveFirst
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Pawan KumarDatabase ExpertCommented:
Please provide full code.
0
Mel BrooksCITOAuthor Commented:
Here is the code in it's entirety:

Private Function LoadDefaultDepartments(strLoginComputer As String) As Boolean
On Error GoTo err_Load
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command

cn.ConnectionString = strDBConnection
cn.Open
cmd.ActiveConnection = cn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "qdfSiteLogin"
cmd.Parameters(1) = strLoginComputer
Set rs = cmd.Execute
If rs.EOF = False Then
  rs.MoveFirst
    strPublicLocation = rs!DeptCode
  intTimeZoneAdjust = rs!TimeZoneAdjust
  If Not IsNull(rs!DeptCode2) Then
     strPublicLocation2 = rs!DeptCode2
     If Not IsNull(rs!DeptCode3) Then
        strPublicLocation3 = rs!DeptCode3
        If Not IsNull(rs!DeptCode4) Then
            strPublicLocation4 = rs!DeptCode4
            If Not IsNull(rs!DeptCode5) Then
                strPublicLocation5 = rs!DeptCode5
            End If
        End If
    End If
  End If
  rs.Close
  LoadDefaultDepartments = True
Else
    rs.Close
    LoadDefaultDepartments = False
End If
cn.Close

ExitFunction:
    Set rs = Nothing
    Set cmd = Nothing
    Set cn = Nothing
    Exit Function
   
err_Load:
    If Right(CurrentDb.NAME, 5) = "accdb" Then
        MsgBox Err.Number & " " & Err.Description
     End If
    LoadDefaultDepartments = False
    Resume ExitFunction
   
End Function
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
ADODB Recordsets often return 0 or -1 (or an invalid number) for the Recordcount property. If you want to know how many records your ADO recordset is returning, you should issue a COUNT statement using the same filters as your SELECT statement.

In you second set of code, you're not referring to the Recordcount property, and that would be the correct way to work with an ADO recordset. Better yet:

If Not (rs.EOF and rs.BOF) Then
  '/ you have records
End If

You also don't need to issue a MoveFirst. And ADO recordset will always be on the first record (unlike some older DAO recordsets, which you had to populate fully before using).
0
Mel BrooksCITOAuthor Commented:
Scott.  This function will work fine if I exclude the line rs.MoveFirst and accept the values in the query fields, but there are other sections of code where the query returns more than one record.  In the instances  I use rs.MoveLast and then rs.RecordCount to get the total number of records. Even with multiple records the recordcount property is now returning -1, not the total number of records.  To me, this now seems like a permissions issue in SQL server where SA has permission to return the count of records that are queried and the Windows user account will only get a True or False result if there are records in the query result.  Does anyone know of setting I can change in SQL Management studio that would make sql server return the number of records for the Windows user account -- assuming that what I am surmising is correct?
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Even using rs.MoveLast is not guaranteed to get you the true recordcount for an ADO recordset. Using a ForwardOnly cursor, for example, will not return an accurate record count. The only way to get an accurate count with an ADO recordset is to use a clientside cursor (which forces the recordset to fetch all the records) or use OpenKeyset or OpenStatic cursors.

There is nothing in SQL or management studio that controls the way in which records are retrieved. That is a factor of (a) your code and (b) the provider you use. If this behavior started suddenly, I'd suspect perhaps your provider was upgraded, or something along those lines.

REgardless, you should refactor your code to use proper record techniques, which would include other methods to count your records (if needed).
0
Mel BrooksCITOAuthor Commented:
Scott. You must not have read the very first part of this question.  The recordcount property works fine when using a connection string that uses sql authentication to connect to the database, but is failing when using a connection string that relies on Windows authentication.
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
for your connection object, try specify the CursorLocation, like:

cn.CursorLocation = adUseClient
cn.Open

Open in new window


this will enable you to have a correct record count returned.
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
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
The providers are different, which may account for your troubles. Your original uses SQLOLEDB.1, while the trusted connection one uses SQLOLEDB. I believe the SQLOLEDB.1 refers to a specific version of that provider.
0
Mel BrooksCITOAuthor Commented:
It looks like your line of code did the trick.  the program is now returning an integer value rather than a true false.
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
ADODB

From novice to tech pro — start learning today.