Import CSV file to an access table

Per_

Board Regular
Joined
Sep 16, 2011
Messages
90
Hello all, I would like to import an CSV file (see below) to an table in access with the command below. I only get one field name in the table with the
name: Header1;Header2. How can I get two field name: Header1 and Header2?

/P

Command
========
VBA Code:
DoCmd.TransferText _
        TransferType:=acImportDelim, _
        TableName:="TEST", _
        FileName:="C:\TEST.CSV", _
        HasFieldNames:=True

CSV file
=======
Code:
Header1;Header2
1;2
 
Last edited by a moderator:

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
1,270
You need an "Import Specification" since CSV file is semicolon delimited.

To create the specification you need to attempt importing the data manually once, so you can save the specification, and use it with the VBA code later.
  1. Select External Data -> New Data Source -> From File -> Text File
  2. Browse and select the CSV file to import into your table.
  3. Then the Import Text Wizard dialog is opened. Set the import properties, but do not close the wizard yet.
  4. Click on Advanced button at the bottom left. This opens the specification dialog.
  5. Click Save As, name it properly, for example "SemiColonImportSpec", and save it.
  6. Now you can cancel the import since all we need was saving the specification file.

Go back to your VBA code, and include one more parameter, which is called : SpecificationName:

VBA Code:
DoCmd.TransferText _
TransferType:=acImportDelim, _
SpecificationName:="SemiColonImportSpec", _
TableName:="TEST", _
FileName:="C:\TEST.CSV", _
HasFieldNames:=True
 

Watch MrExcel Video

Forum statistics

Threads
1,122,364
Messages
5,595,722
Members
414,013
Latest member
tnobbs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top