Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export tables to an excel spreadsheet in same directory

Tags:

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... and then choosing the format and typing in the file name.

However, this way the user actually has to type the name in so there is room for misnaming the file or for saving it as the wrong format or in the wrong location. Also, the two tables have to be exported to two separate workbooks.

What I want to be able to do is make a button on a form that automatically exports one table to one worksheet and the other to another worksheet, both in the same excel workbook.

If putting them in the same workbook isn't possible, that's fine. I just want them to automatically be exported to the same directory my access database is saved in.

If you know how to do it, an added perk might be to customize the name to include the date. That way the directory would have historical exports as well. Any advice?

like image 296
Matt Avatar asked Dec 04 '09 20:12

Matt


People also ask

How do I export data table to Excel?

On the External Data tab, in the Export group, click Excel. In the Export - Excel Spreadsheet dialog box, review the suggested file name for the Excel workbook (Access uses the name of the source object). If you want, you can modify the file name. In the File Format box, select the file format that you want.

Can I copy a table from one Excel sheet to another?

Keyboard shortcut: Press CTRL+Spacebar, on the keyboard, and then press Shift+Spacebar. Copy all the data on the sheet by pressing CTRL+C. Click the plus sign to add a new blank worksheet. Click the first cell in the new sheet and press CTRL+V to paste the data.


1 Answers

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

like image 59
Lawrence P. Kelley Avatar answered Sep 21 '22 19:09

Lawrence P. Kelley