lcha
asked on
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!
lcha
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
ASKER
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?
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
hth
Rob
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
hth
Rob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Rob
>>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.
That shouldn't be a problem. Access is designed as a multiuser database for multiple simaltaneous connections.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Open in new window
HTH
Scot