MS excel - automate copying multiple query results in MS access to ranges of cells

Dear Experts,

I need assistance to automate copying the output results of multiple queries in MS Access to specific ranges of cells in an open MS Excel document.

Would someone have some insight or be able to let me know how this can be done?

Thanks for your help!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Not knowing what your query data looks like, single results, list of data, etc... This will get you started opening a workbook and writing data to it.

Sub SendToExcel()
    Dim xlApp As Object
    Dim xlWB As Object
    Dim xlWS As Object
    Dim MyReport As String

    'existing report name - modify as needed
    MyReport = "C:\Users\Scot\Documents\EE\" & "27647339.xlsx"

    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Open(MyReport)
    Set xlWS = xlWB.Worksheets("Sheet1")

    xlWS.[a1] = "Test"

    xlApp.CutCopyMode = False
    xlWB.Close True

    Set xlWS = Nothing
    Set xlWB = Nothing
    Set xlApp = Nothing

    MsgBox "Done"
End Sub

Open in new window


lchaAuthor Commented:
hey, thanks p912s.  So, if my worksheet is already open.   Would there be a way to do this through a vba program that runs from the ms excel worksheet that could copy and paste the table format output results of open MS Access queries to ranges of data on the excel worksheet.  Is there some function I could use to provide all the query names and get those results?

Look up "CopyFromRecordset". Here are some links & code snippets that may point you in the right direction. Note the "###" sections are where you need to add some code for it to become functional.

Sub EgOfCopyFromRecordset()
'sourced from

Dim icol As Long
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim ws As Worksheet

Set rs = New ADODB.Recordset
Set ws = ActiveSheet

'### add a connection string, for an example see

'###provide an sql string for identifying what to populate the recordset with.
'### open the rs
rs.Open '### state the arguments to go with ".open"

'put the headers in row one of the activesheet (based on the ws variable)
For icol = 0 To rs.Fields.Count - 1
    ws.Cells(1, icol + 1).Value = rs.Fields(icol).Name
Next icol

ws.Range(ws.Cells(1, 1), ws.Cells(1, rs.Fields.Count)).Font.Bold = True
ws.Range("A2").CopyFromRecordset rs

Set cnn = Nothing
Set rs = Nothing
Set ws = Nothing

End Sub

Open in new window

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That's a good point Scot. Using the built-in functionality is a much better idea. I wonder how the OP would do it though, perhaps they may end up experiencing locking errors if they try to have Access open at the same time as Excel.

>>perhaps they may end up experiencing locking errors if they try to have Access open at the same time as Excel.

That shouldn't be a problem. Access is designed as a multiuser database for multiple simaltaneous connections.
Sorry about hijacking this thread...

That's true Scot, I didn't really elaborate on my prefix to that thought when I said "I wonder how the OP would do it though". Am I right in thinking that Access will go into/be in exclusive mode if there is Access code open (or running?) when Excel attempts to use a connection?

Also, my understanding about locking or reading permissions is that the first connection made is the one that defines the type of connection that all subsequent (excel?) connections for the duration of that instance's lifetime. So if the first connection has read/write then so would any second or third connection, which may lead to errors if/when multiple users are accessing the db via excel.
/end edit

Okay, enough hijacking from me... Icha, do you need any more help (from Scot :-))?

lchaAuthor Commented:
hey, thanks Scott and Rob.  Appreciate your help and sorry for the delay.  Thanks for all the tips!  I will go ahead and award the points for now and come back to this later.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.