How to Import CSV Into Access using VBA
- How to Link CSV File Into Access Database
- Semi-Colon Delimited CSV File
- Export a Text File Manually Into MS Access
- Conclusion
VBA provides developers with a variety of different functions and features. This article will guide you on how to import CSV into Access VBA.
How to Link CSV File Into Access Database
Why use VBA code to import data when you have other options available? Importing data through VBA codes helps you navigate and work with various formats that aren’t available when you import data using the visual interface.
Also, on a more practical level, when you have to make the same import a couple of times, importing data with VBA codes is an efficient, more accessible, and faster method to do the job for you.
It creates a function that can be used every time, making the process quicker and easier than going through the import wizard every time.
CSV stands for Comma Separated Values
. This means that the default import functionality looks for commas in the file.
However, if you’re using anything but commas, you can try the following steps to make it work.
-
First, import the file manually.
-
Once you have specified the settings, just before selecting
Finish
, selectAdvanced
to alter a few things. -
Select
Save As
and either select a name proposed by Access or specify a name yourself. -
Make sure to take note of the name you choose. For example,
"Specification of Data Import"
. -
Select
OK
two times and then cancel the import. -
Now, use the import specification you created with the code below.
DoCmd.TransferText acImportDelim, "Specification of Data Import", "T1", "D:\Data.csv", False
One of the easiest ways to import CSV into Access VBA is by linking the CSV file into the Access database as a table. You can then work on these tables like any other ordinary Access tables.
For example, you can create an appropriate query based on the table, which will return what you want.
The table can either be linked manually or with VBA in the following manner:
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tableToImport", _
FileName:="C:\MyData.csv", HasFieldNames:=true
Then, run the following code to re-link the CSV tables and perform the import.
Dim DB As DAO.Database
' Re-linking the CSV Table
Set DB = CurrentDb
On Error Resume Next: DB.TableDefs.Delete "tableToImport": On Error GoTo 0
DB.TableDefs.Refresh
DoCmd.TransferText TransferType:=acLinkDelim, TableName:="tableToImport", _
fileName:="C:\MyData.CSV", HasFieldNames:=True
DB.TableDefs.Refresh
' Now perform the import
DB.Execute "INSERT INTO anyTable SELECT c1, c2, ... FROM tableToImport " _
& "WHERE NOT F1 IN ('A1', 'A2', 'A3')"
DB.Close: Set DB = Nothing
If you need to import some rows from the beginning and some from the end into Access, that can also be done. Once you have linked the CSV file into Access, you can import any required rows with the following:
INSERT INTO anyTable SELECT c1, c2, ... FROM tableToImport WHERE NOT F1 IN ('A1', 'A2', 'A3')
In the statement, rows with values 'A1'
, 'A2'
, and 'A3'
in column F1
are skipped and excluded from the final result set. However, if your CSV file has field names, it is important to note that if you import with HasFieldNames:=true
, these names will be used as column names.
Otherwise, Access assigns column names on its own, for example 'F1'
, 'F2'
, 'F3'
, and so on. If your file is smaller, lines can easily be read and written quickly.
Semi-Colon Delimited CSV File
If your CSV file is semicolon-delimited, you need an Import Specification
. To create this certain specification, you must attempt to import the data manually once.
This allows you to save the specification and use it with the VBA code later. This can be done in the following way:
-
Select
External Data
>New Data Source
>From File
>Text File
-
Browse through the options and select the CSV file to import into your table.
-
This prompts the
Import Text Wizard
dialogue to open. -
Set import properties accordingly; however, do not close the wizard just yet.
-
Click on the
Advanced
button at the bottom left. This opens the specification dialog. -
Click
Save As
, name it properly like"SemiColonImportSpecification"
, and save the final file. -
You can now cancel the import since now all we need to do here is save the specification file.
-
Go back to your initial VBA code and include one more parameter called
specifications
in the following way:DoCmd.TransferText _ TransferType:=acImportDelim, _ SpecificationName:="SemiColonImportSpec", _ TableName:="TEST", _ FileName:="C:\TEST.CSV", _ HasFieldNames:=True
Export a Text File Manually Into MS Access
It is also possible to import a text file manually into MS Access. This allows you to choose your text delimiters and cell delimiters.
For this to work, you need to choose external data from the menu, select the file you intend to use and step through the wizard.
Once you get the imports working through the wizards, you can easily save a particular import specification and use it for your next DoCmd.TransferText
. Doing this will allow you to have nonstandard delimiters such as single quotes text and semicolons.
Conclusion
Importing CSV into Access VBA can be complicated; however, if you know the correct technique, the process becomes much more straightforward. This article outlines the exact methods and details on how to do this correctly.
Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!
GitHub