Export tables to an excel spreadsheet in same directory

前端 未结 3 1782
孤独总比滥情好
孤独总比滥情好 2020-12-05 07:25

I have two tables in my access database that I want to be able to export to excel.

I can do it by opening the table and then doing File->Export...

相关标签:
3条回答
  • 2020-12-05 07:47

    You can use VBA to export an Access database table as a Worksheet in an Excel Workbook.

    To obtain the path of the Access database, use the CurrentProject.Path property.

    To name the Excel Workbook file with the current date, use the Format(Date, "yyyyMMdd") method.

    Finally, to export the table as a Worksheet, use the DoCmd.TransferSpreadsheet method.

    Example:

    Dim outputFileName As String
    outputFileName = CurrentProject.Path & "\Export_" & Format(Date, "yyyyMMdd") & ".xls"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table1", outputFileName , True
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "Table2", outputFileName , True
    

    This will output both Table1 and Table2 into the same Workbook.

    HTH

    0 讨论(0)
  • 2020-12-05 07:58

    Lawrence has given you a good answer. But if you want more control over what gets exported to where in Excel see Modules: Sample Excel Automation - cell by cell which is slow and Modules: Transferring Records to Excel with Automation You can do things such as export the recordset starting in row 2 and insert custom text in row 1. As well as any custom formatting required.

    0 讨论(0)
  • 2020-12-05 08:09

    For people who find this via search engines, you do not need VBA. You can just:

    1.) select the query or table with your mouse
    2.) click export data from the ribbon
    3.) click excel from the export subgroup
    4.) follow the wizard to select the output file and location.
    
    0 讨论(0)
提交回复
热议问题