Home >Database >Mysql Tutorial >How to Fix Run-time Error 3704 When Accessing SQL Databases in Excel VBA?
In accessing data from a SQL database using VBA code, you may encounter an error when attempting to copy data from the recordset to Excel. Specifically, the error "Run-time error 3704: Operation is not allowed when object is closed" occurs when the recordset object is closed.
To resolve this issue:
objMyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDatabase;User ID=abc;Password=abc;"
Const adCmdText As Long = 1 Dim strSQL As String ... // Other code above ' Open Recordset' Set objMyRecordset.ActiveConnection = objMyConn strSQL = "select * from myTable" objMyRecordset.Open strSQL ... // Rest of the code below
By making these adjustments, you should be able to successfully copy data from the SQL database to Excel using VBA without encountering the Run-time error 3704.
The above is the detailed content of How to Fix Run-time Error 3704 When Accessing SQL Databases in Excel VBA?. For more information, please follow other related articles on the PHP Chinese website!