Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 279429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:01:54+00:00 2026-05-12T05:01:54+00:00

I am copying an VBA code snippet from MSDN that shows me how to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:01:55+00:00Added an answer on May 12, 2026 at 5:01 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote VBA code that updates changes made in Excel to a database (SQL
I am copying code from website matplotlib and pasting into the Vim editor in
I'm currently considering a number of options for copying an excel sheet into a
When copying and pasting a bit of sample code from MSDN, I came up
i am copying cells from excel into an open word document. the way i
I am copying projects in one solution that is bound to VSS from one
I am copying data from MS Access to SQL Server using SSIS. Only one
Im copying data from a ms access db to sql server like this... string
I'm copying some objects into a file and they all are derieved from same
I'm copying files from the temporary internet files cache into a folder, in bulk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.