We help IT Professionals succeed at work.

recordset.recordcount always shows -1

My record set always returns a -1 when I enter it as a query it returns records.  Why would it not register records in the recordset and register them in the query?
Dim cn As New ADODB.Connection
    Set cn = CurrentProject.Connection
 
    Dim rs As New ADODB.Recordset
    rs.ActiveConnection = cn
 
    rs.Open "SELECT tblDeferalDetails.EventDetailsID, EventDetailsTable.JobReferenceID " & _
            "FROM EventDetailsTable INNER JOIN tblDeferalDetails ON EventDetailsTable.EventID = tblDeferalDetails.EventDetailsID " & _
            "WHERE (((EventDetailsTable.JobReferenceID)= '16080011'));"
    rs.MoveFirst
    Debug.Print rs.RecordCount

Open in new window

Comment
Watch Question

Now try with this.



Dim cn As New ADODB.Connection
    Set cn = CurrentProject.Connection
 
    Dim rs As New ADODB.Recordset
    rs.ActiveConnection = cn
    rs.CursorLocation = adUseClient
   rs.CursorType = adOpenStatic
 
    rs.Open "SELECT tblDeferalDetails.EventDetailsID, EventDetailsTable.JobReferenceID " & _
            "FROM EventDetailsTable INNER JOIN tblDeferalDetails ON EventDetailsTable.EventID = tblDeferalDetails.EventDetailsID " & _
            "WHERE (((EventDetailsTable.JobReferenceID)= '16080011'));"
    rs.MoveFirst
    Debug.Print rs.RecordCount

Open in new window

Commented:
Use a cursor type that allows you to MoveLast first, then MoveFirst (I forget what the other options are).  The problem is that the cursor hasn't fetched all the data yet, so, it doesn't know how many records there are.  If you MoveLast, then MoveFirst, it will have retrieved all the data and then your recordcount should be OK.
set nocount off

run that statement

Explore More ContentExplore courses, solutions, and other research materials related to this topic.