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
TIMASAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
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
 
Pawan KumarDatabase ExpertCommented:
Please try this --

Provider=sqloledb;Data Source=LBApp1;Initial Catalog=myDataBase;
Integrated Security=SSPI;
0
 
Ryan ChongCommented:
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
TIMASAuthor 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
 
Pawan KumarDatabase ExpertCommented:
Please provide full code.
0
 
TIMASAuthor 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
 
TIMASAuthor 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
 
TIMASAuthor 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
 
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
 
TIMASAuthor 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
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.

All Courses

From novice to tech pro — start learning today.