Using VBA code to import a CSV file into an Access table automates data entry, eliminates manual copy-paste errors, and ensures consistency when updating records regularly. This method works best when you have a fixed CSV structure and a matching Access table schema. Before starting, ensure the CSV file is properly formatted with headers in the first row and no mixed data types in columns, and that the target Access table already exists with matching field names and data types.
What You'll Need
- Microsoft Access with VBA support enabled (Access 2010 or later)
- A CSV file with consistent structure and headers
- An existing Access table with field names matching the CSV headers
- File path to the CSV file (full path, not relative)
- Administrator or edit permissions to the Access database
Step-by-Step
- Open your Access database and press
Alt + F11to open the Visual Basic for Applications (VBA) editor. - In the VBA editor, go to Insert > Module to create a new standard module. This is where you will paste the code.
- Copy and paste the following VBA code into the module:
Sub ImportCSVToTable()
Dim csvPath As String
Dim tableName As String
' Define the full path to your CSV file
csvPath = "C:\YourFolder\YourFile.csv"
' Define the exact name of the target table in Access
tableName = "YourTableName"
' Delete existing data in the table (optional, for fresh imports)
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE * FROM " & tableName
DoCmd.SetWarnings True
' Import the CSV file into the specified table
DoCmd.TransferText acImportDelim, , tableName, csvPath, True
End Sub
- In the code, replace
C:\YourFolder\YourFile.csvwith the full, correct file path to your CSV. Use double backslashes (\\) in Windows paths. - Replace
"YourTableName"with the exact name of the Access table you want to import into. The table must exist and have fields with names that match the CSV headers exactly, including case and spacing. - Save the module by clicking File > Save and give it a name like
modImportCSV. - Close the VBA editor and return to Access.
- Press
Alt + F8to open the Macro dialog, selectImportCSVToTable, and click Run. - Wait a few seconds. Access will import the data and display no confirmation by default. Check the table manually to verify records appear.
Common Mistakes
- Using a relative file path (e.g., "Data\file.csv") instead of the full absolute path. VBA cannot resolve relative paths reliably.
- Mismatched field names between CSV headers and table columns. Even extra spaces or different capitalization (e.g., "FirstName" vs. "firstname") will cause import failure.
- Not deleting existing data before import, leading to duplicate records if the table isn’t designed for append-only operations.
- Importing into a table that has required fields not present in the CSV, or with incompatible data types (e.g., trying to import text into a Number field).
- Using a CSV with unescaped commas inside quoted text fields without proper formatting — Access expects standard CSV quoting rules.
Tips
- Test the import with a small subset of your CSV data first to confirm structure and mapping.
- Always back up your Access database before running import routines.
- If your CSV uses semicolons instead of commas (common in European locales), use
acImportDelimwith a custom import specification created manually in Access first. - For recurring imports, create a button on a form and assign the macro to it for one-click execution.
- Use
DoCmd.SetWarnings Falseto suppress confirmation prompts during bulk operations, but always turn them back on afterward to avoid accidentally deleting data.
Troubleshooting
If the import fails, first check the following:
- Ensure the CSV file is not open in Excel or another program — Access needs exclusive file access.
- Verify the file path is correct by pasting it into Windows Explorer to confirm the file exists.
- Check field names in Access and CSV: open the CSV in Notepad to see exact headers. Compare each one with the table design in Access (View > Design View).
- Confirm the target table has no primary key or unique index that conflicts with incoming data — duplicate primary keys will cause errors.
- If you see an error about "too few fields," the CSV may have inconsistent row lengths. Open it in Excel, save again as UTF-8 CSV, and retry.
- If Access reports "cannot find the input table or query," the table name is misspelled or doesn’t exist. Double-check the name in the Navigation Pane.
- If errors persist, temporarily remove the
DELETEline and try importing into a new, empty table with the same structure to isolate the issue.
Frequently Asked Questions
Can I import a CSV with dates in different formats?
Yes, but Access may misinterpret them. Ensure the CSV uses a standard format like YYYY-MM-DD or MM/DD/YYYY. You can also import into a Text field first, then use an update query to convert to Date/Time.
Do I need to have Excel installed?
No. VBA’s TransferText method uses Access’s built-in text importer and does not require Excel.
Can I import multiple CSV files at once?
Yes. Wrap the code in a loop that iterates through a folder using the Dir() function. You’ll need to modify the code to handle file enumeration.
Why does my import skip rows?
Rows are skipped if the CSV has blank lines at the top, inconsistent delimiters, or if a field contains unquoted commas. Clean the file in Excel or Notepad++ before importing.
Can I import into a linked table?
No. TransferText only works with local Access tables. If the table is linked to an external source like SQL Server, you must import into a local copy first.
What if my CSV has more columns than the Access table?
Extra columns are ignored automatically. As long as the matching fields are present, the import will succeed. You can safely leave unused columns in the CSV.