adodb recordset returning -1 when using Trusted Connection

Mel Brooks
Mel Brooks used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Please try this --

Provider=sqloledb;Data Source=LBApp1;Initial Catalog=myDataBase;
Integrated Security=SSPI;
Ryan ChongSoftware Team Lead

Commented:
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

Author

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
Ensure you’re charging the right price for your IT

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

Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Please provide full code.

Author

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
Most Valuable Expert 2012
Top Expert 2014

Commented:
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).

Author

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?
Most Valuable Expert 2012
Top Expert 2014

Commented:
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).

Author

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.
Software Team Lead
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.
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.

Author

Commented:
It looks like your line of code did the trick.  the program is now returning an integer value rather than a true false.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial