Others

Use an Access VBA File Picker to Import an Excel Workbook

Maira MehtabMaira Mehtab Nov 27, 2026 869 views

Question details

Let users select an Excel workbook and specify the destination table when importing it from an Access form.

Use an Access VBA File Picker to Import an Excel Workbook
Product
Microsoft Access and Microsoft Excel
Device & OS
not provided
Scenario
An Access form button should replace a fixed-file TransferSpreadsheet macro with an interactive import.
Observed behavior
The existing macro imports a fixed Excel file; the desired workflow prompts for a workbook and destination table.
Before you start

Check that the workbook has column headers in its first row and that its data is suitable for the destination table. Open the Access database in a trusted location or enable its content so the button's VBA code can run.

Solution 1Recommended

Add a VBA file picker to an Access form button

Use this method to let users browse for an Excel workbook and enter a destination table name before importing.

The example supports .xlsx and .xls workbooks and treats the first row as column headers. If the named table already exists, Access attempts to append the data, so its fields and data types must be compatible.

1
Create the import button

Open the form in Design View, add a Button from the Design tab, and set its Name property to cmdImport in the Property Sheet.

2
Open the click event

Select cmdImport, open the Event tab in the Property Sheet, and choose [Event Procedure] for On Click. Click the ellipsis button to open the form's VBA module.

3
Add the import code

Replace the generated cmdImport_Click procedure with this code: Private Sub cmdImport_Click() Dim picker As Object Dim filePath As String Dim tableName As String Dim spreadsheetType As Long On Error GoTo ImportError Set picker = Application.FileDialog(3) With picker .Title = "Select an Excel workbook" .AllowMultiSelect = False .Filters.Clear .Filters.Add "Excel workbooks", "*.xlsx;*.xls" If .Show <> -1 Then Exit Sub filePath = .SelectedItems(1) End With tableName = Trim$(InputBox("Enter the destination table name:")) If tableName = "" Then Exit Sub If LCase$(Right$(filePath, 4)) = ".xls" Then spreadsheetType = acSpreadsheetTypeExcel8 Else spreadsheetType = acSpreadsheetTypeExcel12Xml End If DoCmd.TransferSpreadsheet acImport, spreadsheetType, tableName, filePath, True MsgBox "Import completed.", vbInformation Exit Sub ImportError: MsgBox "Import failed: " & Err.Description, vbExclamation End Sub

4
Test the import

Save the form, switch to Form View, and click cmdImport. Select an .xlsx or .xls file, enter a table name, and check the imported records in the Navigation Pane.

Existing queries: The example imports the workbook but does not run the original macro's query. If you still need that query, add DoCmd.OpenQuery after TransferSpreadsheet and verify that it references the intended destination table.
Free Microsoft Office alternative

Prepare Excel Workbooks for Import with WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar interface for reviewing and editing Excel workbooks before an Access import. Its format compatibility makes it easy to move existing spreadsheets into your workflow. WPS does not run or fix Access database macros.

Review column headers and spreadsheet data before importingOpen and edit Microsoft Excel .xlsx and .xls filesUse familiar spreadsheet controls when moving from Microsoft OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

How do I choose the destination table during the import?

The recommended button code uses InputBox to ask for a table name and passes that name as the table argument to DoCmd.TransferSpreadsheet.

What happens if the user cancels the file picker?

The code exits the button procedure without importing. It also exits if the table-name prompt is canceled or left blank.

Can I import into an existing Access table?

Yes. Enter the existing table name when prompted. Access attempts to append the records, so check that the workbook columns and data types are compatible with the table.

How can I run my existing query after the import?

Add DoCmd.OpenQuery with the query name after the TransferSpreadsheet line. Check that the query references the table you want to update, especially if users can enter different destination names.

Why does the import button not run?

Check the button's On Click property in the form's Property Sheet and make sure it is set to [Event Procedure]. Also check whether Access has blocked the database content; use a trusted location or enable content if appropriate.