I am copying an VBA code snippet from MSDN that shows me how to grab results from a SQL query into excel sheet (Excel 2007):
Sub GetDataFromADO() 'Declare variables' Set objMyConn = New ADODB.Connection Set objMyCmd = New ADODB.Command Set objMyRecordset = New ADODB.Recordset 'Open Connection' objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;User ID=abc;Password=abc;" objMyConn.Open 'Set and Excecute SQL Command' Set objMyCmd.ActiveConnection = objMyConn objMyCmd.CommandText = "select * from myTable" objMyCmd.CommandType = adCmdText objMyCmd.Execute 'Open Recordset' Set objMyRecordset.ActiveConnection = objMyConn objMyRecordset.Open objMyCmd 'Copy Data to Excel' ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset) End Sub
I have already added Microsoft ActiveX Data Objects 2.1 Library under as a reference. And this database is accessible.
Now, when I run this subroutine, it has an error:
Run-time error 3704: Operation is not allowed when object is closed.
On the statement:
ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset)
Any idea why?
Thanks.
To connect Excel to a database in SQL Database, open Excel and then create a new workbook or open an existing Excel workbook. In the menu bar at the top of the page, select the Data tab, select Get Data, select From Azure, and then select From Azure SQL Database.
Microsoft Excel can be a powerful tool to evaluate SQL data. In Excel, a connection can be created to directly link to a particular database filtered according to your requirements. This allows you to report SQL data, attach a table of data into Excel, create a pivot table and have better manipulation of your SQL data.
I've added the Initial Catalog to your connection string. I've also abandonded the ADODB.Command syntax in favor of simply creating my own SQL statement and open the recordset on that variable.
Hope this helps.
Sub GetDataFromADO() 'Declare variables' Set objMyConn = New ADODB.Connection Set objMyRecordset = New ADODB.Recordset Dim strSQL As String 'Open Connection' objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDatabase;User ID=abc;Password=abc;" objMyConn.Open 'Set and Excecute SQL Command' strSQL = "select * from myTable" 'Open Recordset' Set objMyRecordset.ActiveConnection = objMyConn objMyRecordset.Open strSQL 'Copy Data to Excel' ActiveSheet.Range("A1").CopyFromRecordset (objMyRecordset) End Sub
Suggested changes:
CopyFromRecordset
;Revised code:
Sub GetDataFromADO() 'Declare variables' Dim objMyConn As ADODB.Connection Dim objMyCmd As ADODB.Command Dim objMyRecordset As ADODB.Recordset Set objMyConn = New ADODB.Connection Set objMyCmd = New ADODB.Command Set objMyRecordset = New ADODB.Recordset 'Open Connection' objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;User ID=abc;Password=abc;" objMyConn.Open 'Set and Excecute SQL Command' Set objMyCmd.ActiveConnection = objMyConn objMyCmd.CommandText = "select * from mytable" objMyCmd.CommandType = adCmdText 'Open Recordset' Set objMyRecordset.Source = objMyCmd objMyRecordset.Open 'Copy Data to Excel' ActiveSheet.Range("A1").CopyFromRecordset objMyRecordset End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With