Excel VBA Macro to copy Access Query into Excel

JoyC

Board Regular
Joined
Aug 28, 2009
Messages
91
I have an Access Database which I use for controlling and calculating large volumes of data (too big for Excel to cope with). However the summaries from this data (a series of queries and crosstabs) for this need to be put into Excel for reporting purposes. I would like to be able to click a command button in Excel and pull the data over from the database. The macro I currently have in my Excel workbook, which has been gathered through research on other forums, does not pull the headings over:

HTML:
Sub Query()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Range("A:F").Select
With Selection.ClearContents
End With
Set db = OpenDatabase("C:\Personal Data\zgd81463\Training\Training.mdb")
Set rst = db.OpenRecordset("X_ProjectGroup")
ActiveSheet.Range("A2").CopyFromRecordset rst
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing

End Sub

Could someone please help with applying the headings to the macro.

Thank you.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
something like this (not tested)
Code:
Sub Query()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Range("A:F").Select
With Selection.ClearContents
End With
Set db = OpenDatabase("C:\Personal Data\zgd81463\Training\Training.mdb")
Set rst = db.OpenRecordset("X_ProjectGroup")

For iCol = 1 To rst.Fields.Count
    ActiveSheet.Cells(1, iCol) = rst.Fields(iCol - 1).Name
    Next iCol

ActiveSheet.Range("A2").CopyFromRecordset rst
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing

End Sub
 
Upvote 0
Thank you.

Unfortunately, I am now getting a "Compile error: Type mismatch" message on the "For iCol = 1 To rst. Fields.Count" line. Any ideas?
 
Upvote 0
try dimming iCol as integer.
If that doesn;t work, can';t help any more, sorry..
 
Upvote 0

Forum statistics

Threads
1,206,830
Messages
6,075,113
Members
446,123
Latest member
junkyardforme

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top