Exporting entire Access database into Excel

Coelho_c

New Member
Joined
Feb 3, 2003
Messages
10
I have an Access database with many tables and queries which needs to be exported into Excel. IS there a quicker way of exporting this into Excel than doing each object seperately?
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi,

One way is to use a macro. The following code will only work in Access 2000 along with Excel 2000 (or XP). Just add this code to a module in your database and run it. Let me know how you get on :)

Code:
Option Explicit

Sub ExportAll()
    Dim oXLApp As Object
    Dim oADOXCat As Object, oADOXTable As Object
    Dim lngFieldCount As Long
    Dim oADORs As Object

    'Code uses ADO and ADOX to export all tables and queries into and new
    'Excel workbook (one table/query per worksheet).

    'WILL ONLY WORK WITH ACCESS 2000/EXCEL 2000 OR LATER

    'Daniel Klann March 2003

    'Establish the connection to this database
    Set oADOXCat = CreateObject("ADOX.Catalog")
    oADOXCat.ActiveConnection = CurrentProject.Connection

    'Get an instance of Excel to use
    On Error Resume Next
    Set oXLApp = GetObject(, "Excel.Application")
    If Err.Number <> 0 Then Set oXLApp = CreateObject("Excel.Application")
    On Error GoTo 0
    oXLApp.Workbooks.Add

    For Each oADOXTable In oADOXCat.Tables

        If oADOXTable.Type = "ACCESS TABLE" Or oADOXTable.Type = "VIEW" Then    'Table or query

            'Get the data from that particular table
            Set oADORs = CreateObject("ADODB.Recordset")
            oADORs.Open "SELECT * FROM [" & oADOXTable.Name & "]", CurrentProject.Connection, 0, 1

            'Dump the query or table into a new worksheet in the active workbook
            oXLApp.Worksheets.Add

            With oXLApp.ActiveSheet

                'This will put the table/query field names into the Excel worksheet
                For lngFieldCount = 0 To oADORs.Fields.Count - 1
                    .Cells(1, lngFieldCount + 1).Value = oADORs.Fields(lngFieldCount).Name
                Next lngFieldCount

                'This will copy the data into the worksheet
                .Range("A2").CopyFromRecordset oADORs

            End With

        End If

    Next

    oXLApp.UserControl = True
    oXLApp.Visible = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,221,512
Messages
6,160,237
Members
451,632
Latest member
purpleflower26

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