VB6 ADODB.Recordset RecordCount property always returns -1 VB6 ADODB.Recordset RecordCount property always returns -1 database database

VB6 ADODB.Recordset RecordCount property always returns -1


Actually the CursorLocation plays a major role in this case. Use rs.CursorLocation = adUseClient to set the cursor location and try.

    Set rs = New ADODB.Recordset    rs.CursorLocation = adUseClient    Dim DbConnectionString As String    DbConnectionString = mSqlProvider & _                            mSqlHost    Set mDbConnection = New ADODB.Connection    mDbConnection.CursorLocation = adUseServer    Call mDbConnection.Open(DbConnectionString)    If mDbConnection.State = adStateOpen Then        Debug.Print (" Database is open")        ' Initialise the command object        Set mCmd = New ADODB.Command        mCmd.ActiveConnection = mDbConnection        mCmd.CommandText = "select * from myTestTable"        mCmd.CommandType = adCmdText        Set rs = mCmd.Execute        Debug.Print rs.RecordCount  ' This should now return the right value.        Debug.Print rs.Fields(0)   ' returns correct data for first row, first col        Debug.Print rs.Fields(1)   ' returns correct data for first row, 2nd col        Debug.Print rs.Fields(2)   ' returns correct data for first row, 3rd col    End IfEnd Sub


That's a result of the type of cursor used to access the data, this post covers the issue and possible fixes.

http://www.devx.com/tips/Tip/14143

EDIT

I apologize for not being more attentive to the fact that you were dealing with Compact. With Compact the situation is similar to the one I referenced, as it uses forward only cursors by default (which do not support row count) but there are two other cursor types available as documented in the link below.

http://support.microsoft.com/kb/272067


With Compact the default cursor attribute is adOpenForwardOnly for improved performance. As such RecordCount is returned as "-1" which means its not available, rather than blank. This is by design because the # of records in a dynamic cursor could change and result in pinging back and forth between the client server to maintain accuracy. However, if the record count is vital try setting it to use adOpenKeyset or adOpenStatic with a server-side cursor.