I used the below vba code in Excel 2007 to query Access 2007 accdb database successfully. Credit (Taken from "The Excel® Analyst’s Guide to Access" by Michael Alexander). However when I tried same for Excel 2010 with Access 2010 accdb database I had problem. First the system said DAO.Database is not defined in Excel 2010 VBA. So I went to references and ticked DAO 3.6. Next the system said it does not recognize the database at DBEngine.OpenDatabase("M:\SVRData\Booking 2801_BE.accdb"). Please help
Sub RunAccessQuery()
'Step 1: Declare your variables
Dim MyDatabase As DAO.Database
Dim MyQueryDef As DAO.QueryDef
Dim MyRecordset As DAO.Recordset
Dim i As Integer
'Step 2: Identify the database and query
Set MyDatabase = DBEngine.OpenDatabase("M:\SVRData\Booking 2801_BE.accdb")
Set MyQueryDef = MyDatabase.QueryDefs("Bookings")
'Step 3: Open the query
Set MyRecordset = MyQueryDef.OpenRecordset
'Step 4: Clear previous contents
Sheets("Main").Select
ActiveSheet.Range("A6:K10000").ClearContents
'Step 5: Copy the recordset to Excel
ActiveSheet.Range("A7").CopyFromRecordset MyRecordset
'Step 6: Add column heading names to the spreadsheet
For i = 1 To MyRecordset.Fields.Count
ActiveSheet.Cells(6, i).Value = MyRecordset.Fields(i - 1).Name
Next i
End Sub
Microsoft Office 14.0 Access Database Enginereference.