VBA to import data from access database to excel

aryanaveen

New member
Local time
Today, 13:13
Joined
Aug 8, 2016
Messages
9
HI All,

Please help me in completing the my macro.

I need a VBA code which will copy all tables from access database to Excel file.

Access database path - C:\tracker

Database file name is Naveen.accdb.

Now I need a code which will open Naveen.accdb from c drive and imports tables to new excel file.

Thank you
 
in a macro, use transferspreadsheet.
the last parameter is the sheet name, of the same workbook.
 
Something like:

Code:
Sub ExportTables()

Dim AccessApp As Access.Application
Dim tdef As DAO.TableDef
Set AccessApp = CreateObject("Access.Application")
AccessApp.OpenCurrentDatabase ("C:\tracker\Naveen.accdb")
For Each tdef In AccessApp.CurrentDb.TableDefs
    If Left(tdef.Name, 4) <> "MSys" Then
        AccessApp.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, tdef.Name, "C:\tracker\Naveen.xls"
    End If
Next tdef
AccessApp.CloseCurrentDatabase

End Sub

This puts each table in a different sheet without specifying a range. Talking about the Range argument it says here.

When you export to a spreadsheet, you must leave this argument blank. If you enter a range, the export will fail.
 
Last edited:
Thank you :)

but the codes throws an error at below line

Dim AccessApp As Access.Application

compile error : User-defined type not defined
 
I tested this running it from Access? In which application are you running this?

You could add the reference to Access (Tools, References, select Microsoft Access XX.0 Object Library) or try late binding i.e.,

Edit: Don't use this. See Post #10

Sub ExportTables()

Dim AccessApp As Object
Dim tdef As DAO.TableDef
Set AccessApp = CreateObject("Access.Application")
AccessApp.OpenCurrentDatabase ("C:\tracker\Naveen.accdb")
For Each tdef In AccessApp.CurrentDb.TableDefs
If Left(tdef.Name, 4) <> "MSys" Then
AccessApp.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, tdef.Name, "C:\tracker\Naveen.xls"
End If
Next tdef
AccessApp.CloseCurrentDatabase

End Sub
 
Last edited:
HI All,

Please help me in completing the my macro.

I need a VBA code which will copy all tables from access database to Excel file.

Access database path - C:\tracker

Database file name is Naveen.accdb.

Now I need a code which will open Naveen.accdb from c drive and imports tables to new excel file.

Thank you

Why not record a macro for the first table and then amend to suit for the other tables?
 
aryanaveen, you need to run sneuberg's code from a module in ACCESS, not Excel.

But if you want to run in from Excel then use the following code. My previous post for late binding wasn't complete. I tested this one in Excel

Code:
Sub ExportTables()

Dim AccessApp As Object
Dim tdef As Object
Const acExport = 1
Const acSpreadsheetTypeExcel8 = 8
Set AccessApp = CreateObject("Access.Application")
AccessApp.OpenCurrentDatabase ("C:\tracker\Naveen.accdb")
For Each tdef In AccessApp.CurrentDb.TableDefs
    If Left(tdef.Name, 4) <> "MSys" Then
        AccessApp.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, tdef.Name, "C:\tracker\Naveen.xls"
    End If
Next tdef
AccessApp.CloseCurrentDatabase

End Sub

Note that this just the minimum code needed and doesn't have any error checking.
 
But if you want to run in from Excel then use the following code. My previous post for late binding wasn't complete. I tested this one in Excel

Code:
Sub ExportTables()

Dim AccessApp As Object
Dim tdef As Object
Const acExport = 1
Const acSpreadsheetTypeExcel8 = 8
Set AccessApp = CreateObject("Access.Application")
AccessApp.OpenCurrentDatabase ("C:\tracker\Naveen.accdb")
For Each tdef In AccessApp.CurrentDb.TableDefs
    If Left(tdef.Name, 4) <> "MSys" Then
        AccessApp.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, tdef.Name, "C:\tracker\Naveen.xls"
    End If
Next tdef
AccessApp.CloseCurrentDatabase

End Sub

Note that this just the minimum code needed and doesn't have any error checking.

...And need to add a reference to the Access Object Library in Excel's VBE.
 

Users who are viewing this thread

Back
Top Bottom