Solved

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

Posted on 2012-03-24
8
303 Views
Last Modified: 2012-03-29
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!

lcha
0
Comment
Question by:lcha
  • 3
  • 3
  • 2
8 Comments
 
LVL 18

Expert Comment

by:p912s
ID: 37761792
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
    xlApp.Quit
    Set xlApp = Nothing

    MsgBox "Done"
End Sub

Open in new window


HTH

Scot
0
 

Author Comment

by:lcha
ID: 37761818
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?
0
 
LVL 10

Expert Comment

by:broro183
ID: 37762519
hi,

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.

http://www.exceltip.com/st/Import_data_from_Access_to_Excel_%28ADO%29_using_VBA_in_Microsoft_Excel/427.html

Sub EgOfCopyFromRecordset()
'sourced from http://msdn.microsoft.com/en-us/library/aa223845%28v=office.11%29.aspx

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 http://vbatips.com/2007/12/10/how-to-programatically-retrieve-data-from-a-database/

'###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

rs.Close
Set cnn = Nothing
Set rs = Nothing
Set ws = Nothing

End Sub

Open in new window


hth
Rob
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 18

Accepted Solution

by:
p912s earned 450 total points
ID: 37762601
0
 
LVL 10

Expert Comment

by:broro183
ID: 37765452
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.

Rob
0
 
LVL 18

Expert Comment

by:p912s
ID: 37766763
>>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.
0
 
LVL 10

Assisted Solution

by:broro183
broro183 earned 50 total points
ID: 37770796
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?

Edit:
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 :-))?

Rob
0
 

Author Comment

by:lcha
ID: 37785732
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question