Import and export with Excel

Colleagues send lists as Excel files, accounts want reports back as Excel. With External Data that works once; with TransferSpreadsheet in VBA it works the same way every month.

In short: DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "query", "C:\Data\File.xlsx", True writes a table or query to an Excel file. With acImport Access reads a file in: into a new table or appended to an existing one. True means the first row holds the field names.

Before you start: Recordset.

Try it now

Task Write the procedure ExportOpen: it exports the saved query qOpenOrders as an Excel workbook to C:\Data\Open orders.xlsx, with field names in the first row.

Orders 120 rows, 6 columns
OrderIDCustomerIDOrderDateShippedDateFreightPaymentMethod
11630 Apr 20253 May 20254.90Invoice
22318 May 202526 May 20254.90PayPal
31916 Jul 20264.90Direct debit
41131 Mar 20251 Apr 20250.00Invoice
567 Dec 20259 Dec 20259.90PayPal
61819 May 202626 May 20266.90Direct debit
7715 Jun 202616 Jun 20264.90Credit card
82018 Mar 202519 Mar 20250.00PayPal
91029 Oct 20253 Nov 20250.00Direct debit
10721 Nov 20264.90Invoice
1112 May 202510 May 20259.90Direct debit
12107 Aug 20259 Aug 20250.00Invoice
13317 Sep 202624 Sep 20269.90PayPal
141629 Jan 20261 Feb 20266.90Invoice
15413 Jun 202521 Jun 20254.90PayPal
16123 Nov 202512 Nov 20254.90Invoice
1795 May 20268 May 20269.90PayPal
18926 Sep 20252 Oct 20254.90PayPal
19148 Mar 202610 Mar 20260.00Credit card
2012 Apr 20266.90PayPal
211817 Nov 20264.90PayPal
221127 Jul 20264 Aug 20264.90PayPal
231223 Jun 202628 Jun 20264.90Credit card
241012 Jan 202616 Jan 20260.00Direct debit
25130 Mar 202631 Mar 20264.90Credit card
2633 Mar 20257 Mar 20254.90Invoice
27249 Mar 202511 Mar 20254.90PayPal
28122 Sep 202528 Sep 20254.90PayPal
29118 Feb 202522 Feb 20254.90Invoice
301913 Oct 202518 Oct 20254.90PayPal
311019 Sep 202525 Sep 20256.90PayPal
32619 Jun 202624 Jun 20264.90Credit card
332016 Jun 202622 Jun 20269.90Credit card
341323 May 202627 May 20269.90Direct debit
351624 May 20262 Jun 20264.90PayPal
36149 Nov 202614 Nov 20264.90PayPal
37311 Mar 202618 Mar 20269.90PayPal
38222 Jan 202510 Jan 20250.00Invoice
391221 Oct 202527 Oct 20250.00Invoice
40204 Nov 20265 Nov 20269.90PayPal
411729 Mar 202630 Mar 20264.90PayPal
42136 Jan 20268 Jan 20260.00Invoice
43324 Feb 202626 Feb 20264.90PayPal
44721 Sep 202628 Sep 20264.90Invoice
45217 Apr 202523 Apr 20250.00Direct debit
46727 Apr 202628 Apr 20264.90Invoice
47316 Nov 20266.90Direct debit
48126 Jun 202627 Jun 20264.90Credit card
492429 Apr 202630 Apr 20264.90PayPal
50123 Mar 202528 Mar 20254.90Direct debit
511028 Jan 202631 Jan 20260.00PayPal
52229 May 202615 May 20264.90Direct debit
531029 Dec 20257 Jan 20260.00Credit card
54417 Oct 202618 Oct 20269.90Credit card
552430 Aug 202531 Aug 20256.90Credit card
56629 Jan 20262 Feb 20264.90PayPal
571810 Dec 202513 Dec 20256.90Direct debit
58134 Mar 20259 Mar 20259.90Invoice
591129 Nov 20257 Dec 20256.90Invoice
60930 Oct 20254 Nov 20254.90Invoice
611017 Mar 202619 Mar 20264.90PayPal
62914 Jul 202521 Jul 20254.90PayPal
63824 May 20252 Jun 20250.00Invoice
64429 Sep 20265 Oct 20264.90Invoice
651022 Jan 202627 Jan 20269.90Invoice
66927 Mar 202628 Mar 20269.90Credit card
671411 Sep 202618 Sep 20264.90PayPal
6849 Jun 202616 Jun 20260.00Invoice
691710 Jan 202617 Jan 20269.90Credit card
70211 Apr 20262 Apr 20264.90Credit card
711519 Mar 202521 Mar 20254.90Credit card
72204 Mar 20256 Mar 20254.90Invoice
731024 May 20259.90PayPal
7483 Jan 20254 Jan 20259.90Invoice
752330 Oct 20256 Nov 20259.90Direct debit
762230 Aug 20253 Sep 20256.90Invoice
77218 Nov 202523 Nov 20256.90PayPal
78322 Dec 202526 Dec 20254.90Invoice
792126 May 20266.90Invoice
802423 Feb 202627 Feb 20264.90Direct debit
8125 Jun 20266.90PayPal
821114 Aug 20266.90Invoice
832110 May 202618 May 20269.90Credit card
841312 Feb 202519 Feb 20254.90Credit card
85222 Jan 202630 Jan 20264.90PayPal
86231 Oct 20254 Nov 20259.90Credit card
8769 Mar 202517 Mar 20259.90Credit card
8835 Feb 202514 Feb 20259.90Direct debit
891726 Jan 202529 Jan 20256.90PayPal
901125 Sep 20264.90Direct debit
912223 May 20251 Jun 20254.90Credit card
922029 Jan 20252 Feb 20250.00PayPal
93816 Jul 202618 Jul 20266.90Invoice
9427 Aug 202510 Aug 20250.00Invoice
95226 Jan 20258 Jan 20256.90Credit card
961515 Dec 202524 Dec 20254.90Invoice
97623 Sep 202530 Sep 20256.90Invoice
981010 Sep 202516 Sep 20250.00Invoice
991410 Jan 202613 Jan 20269.90Credit card
1001620 Nov 202524 Nov 20250.00Credit card
1011826 Jul 202627 Jul 20264.90Credit card
102215 May 20259.90Invoice
10348 Feb 202515 Feb 20256.90Credit card
104716 Sep 202617 Sep 20264.90Invoice
105726 Jan 202531 Jan 20254.90Direct debit
106232 Aug 20263 Aug 20266.90Direct debit
1071322 Jan 202626 Jan 20269.90Credit card
10846 Oct 202514 Oct 20254.90Direct debit
109106 Nov 20259 Nov 20254.90Direct debit
110917 Oct 202618 Oct 20269.90PayPal
1112324 May 202625 May 20266.90Direct debit
112231 Aug 20251 Sep 20259.90Direct debit
1132025 Jul 202628 Jul 20260.00Direct debit
114624 Nov 202527 Nov 20259.90Credit card
115827 Nov 202530 Nov 20256.90Credit card
116118 Mar 202525 Mar 20250.00Direct debit
117916 Mar 202519 Mar 20256.90Credit card
118410 Jul 202615 Jul 20260.00Invoice
1191423 Jun 202528 Jun 20254.90Direct debit
120221 Sep 202627 Sep 20264.90Direct debit

The arguments

ArgumentExamplemeans
TransferTypeacImport (0), acExport (1), acLink (2)read in, write out, link
SpreadsheetTypeacSpreadsheetTypeExcel12Xml (10)Excel workbook .xlsx
TableName"Categories"table or query
FileName"C:\Data\List.xlsx"file with path
HasFieldNamesTruefirst row holds field names

Importing into an existing table

If the table exists, Access appends the rows; the headings in Excel must match the fields. If it does not exist, Access creates it: columns with numbers become Number fields (type Double), the rest text fields. Access assigns AutoNumbers itself.

Task A colleague keeps new categories in Excel: C:\Data\New categories.xlsx with the column Category. Write the procedure ImportCategories: it appends the rows to the Categories table.

Categories 4 rows, 2 columns
CategoryIDCategory
1Office
2Tech
3Furniture
4Paper

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Access

  1. Once, without code: the External Data tab, Excel under Import or Export. The wizard asks for the file, the target table and whether the first row holds field names.
  2. Regularly: a procedure with TransferSpreadsheet in a standard module, started from a button or a macro with RunCode.
  3. Text files (CSV) go through DoCmd.TransferText. Where the comma is the decimal separator, as in German regional settings, an import with commas between fields needs an import specification; otherwise Access reports 3441 because the field delimiter matches the decimal separator.

Common mistakes

File not found

If the file is missing, Access reports 3011 (“… could not find the object '…'”). If even the folder does not exist, it reports 3044 “'…' is not a valid path.”

Heading does not match the field

When appending, every column heading must match a field of the table. Rename the column in Excel, or import into a new table first and then append with an append query.

FAQ

Import or link?

An import copies the data once. With acLink the Excel file stays the source and Access reads it on every access; you cannot edit linked Excel data in Access.

Can I import just part of a sheet?

Yes, with the Range argument, such as "Sheet1!A1:C50".

Learn next: Split database

Split a database for several users.

Split database

Write to us

Choose language

Select your preferred language