Recordset always returns -1 (excel VBA) Recordset always returns -1 (excel VBA) vba vba

Recordset always returns -1 (excel VBA)


It is important to specify parameter: CursorLocation = adUseClient in connection object.

dbName = "DbInjectorsCatalog"dbFilePath = "C:\DbInjectorsCatalog.mdf"connStr = "Driver={SQL Server native Client 11.0};" & _          "Server=(LocalDB)\v11.0;" & _          "AttachDBFileName=" & dbFilePath & ";" & _          "Database=" & dbName & ";" & _          "Trusted_Connection=Yes"sqlStr = "Select * from Injectors"Set conn = New ADODB.Connectionconn.ConnectionString = connStr

conn.CursorLocation = adUseClient

conn.OpenSet rs = New ADODB.Recordsetrs.Open sqlStr, connStr, adOpenStatic, adLockBatchOptimistic 

Full working example is here:http://straightitsolutions.blogspot.com/2014/12/read-recordcount-from-adodbrecordset.html


have you tried moving to the last before checking for count

recset.MoveLast

also see if this helps

The RecordCount property will return -1 for a forward-only cursor; the actual count for a static or keyset cursor; and either -1 or the actual count for a dynamic cursor, depending on the data source.

Check this question:

VB6 ADODB.Recordset RecordCount property always returns -1


can you try adding this:

objRS.CursorLocation = adUseClientobjRS.Open strSQL, objConn,,adLockReadOnly, adCmdText

The Cursor position is important.

Hope this helps..