VBA ADODB Record Count

borgek

New Member
Joined
Sep 16, 2012
Messages
3
Hi,
For some reason the record count for one of my sql-queries return -1. I have the exact same connection and query above, which returns the correct number of records. sqlRS.RecordCount returns the correct number of records, while sqlrs_c.RecordCount returns -1 (but ir really returns 5 to the worksheet).

Thanks,

Code:
Sub Demo1()
Dim sqlconn As New ADODB.Connection
Dim sqlRS As New ADODB.Recordset
Dim sqlRS_c As New ADODB.Recordset
Dim sqlRS_i As New ADODB.Recordset
Dim sqlstr As String


'checks to see if sheet "data" and "port" is created
Call check_sheet
Set wsd = ThisWorkbook.Sheets("data")
Set wsp = ThisWorkbook.Sheets("port")


sqlconn = "Provider=sqloledb; Data Source= BORGEK\SQLEXPRESS; Initial Catalog=nbim_dw; Integrated Security = SSPI;"


sqlconn.Open
'dump all data
sqlstr = ("select * from security_list")
Set sqlRS = Nothing
sqlRS.Open sqlstr, sqlconn, adOpenStatic, adLockOptimistic
wsd.Cells.Clear
wsd.Range("A2").CopyFromRecordset sqlRS
For icols = 0 To sqlRS.Fields.Count - 1
    wsd.Cells(1, icols + 1).Value = sqlRS.Fields(icols).Name
Next


'select distinct currencies
sqlstr_c = ("select distinct currency from security_list")
Set sqlRS_c = Nothing
sqlRS_c.Open sqlstr_c, sqlconn, adOpenStatic, adLockOptimistic
'wsd.Cells(1, icols + 4).Value #insert currency
wsd.Cells(2, icols + 4).CopyFromRecordset sqlRS_c


For icurr = 0 To sqlRS_c.RecordCount - 1
    wsp.Cells(2, icurr + 1).Value = wsd.Cells(icurr + 2, icols + 4).Value
Next



wsp.Cells(2, 2).Value = sqlRS_c.RecordCount
wsp.Cells(14, 14).Value = sqlRS_c.RecordCount




sqlRS_c.Close
sqlRS_i.Close
sqlRS.Close
sqlconn.Close


End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
I simplified the code some, so it is easier for you to read.

Code:
Sub test2()
Dim sqlconn As New ADODB.Connection
Dim sqlRS As New ADODB.Recordset
Dim sqlRS_c As New ADODB.Recordset
Dim sqlstr As String
Dim sqlstr_c As String


sqlconn = "Provider=sqloledb; Data Source= BORGEK\SQLEXPRESS; Initial Catalog=nbim_dw; Integrated Security = SSPI;"
sqlconn.Open
sqlstr = ("select * from security_list")
sqlRS.Open sqlstr, sqlconn, adOpenStatic, adLockOptimistic


sqlstr_c = ("select distinct currency from security_list")
sqlRS_c.Open sqlstr_c, sqlconn, adOpenStatic, adLockOptimistic
ThisWorkbook.Sheets("sheet3").Cells(2, 2).Value = sqlRS_c.RecordCount
ThisWorkbook.Sheets("sheet3").Cells(2, 3).Value = sqlRS.RecordCount
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top