Import all CSV files in folder using Access 2010

I'm trying to import all CSV files (bulk import) in a folder, update the file name to each related record (field name: FileName) and then move the CSV files to a archive folder. Thanks
Who is Participating?
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
do you have an Import Specification for importing the csv file?

On Error Resume Next
 Dim csvFile As String, xFile As String, csvFolder As String

  csvFolder = "C:\Folder\"  '<<< CHANGE to CORRECT path
  csvFile = Dir(csvFolder & "*.csv")
  While csvFile <> ""
            docmd.transfertext acimportdelim,"importSpecificationName","tablename",csvFolder & csvFile,true
            currentdb.execute "update tableName set filename='" & csvfile & "' where filename is null"
          Name csvFolder & csvFile As ArchiveFolder & csvFile
          csvFile = Dir()
I suggest instead of importing each file, you should link it and then run one or more append queries to bring the data into your Access tables.  You will have much more control over things that way.

To move a processed file into your archive folder, just use the Name command with a different folder name:

    Name SourceFolder & FileName As DestinationFolder & FileName

Both folders must be on the same drive for Name to work.

In fact, you could combine the function of importing with the rename loop in your last post I answered.  Then, having imported the data from the file with the old name, rename it into the archive folder with the new name:

    Name SourceFolder & OldFileName As DestinationFolder & NewFileName

Graham Mandeno [Access MVP 1996-2015]
Rey Obrero (Capricorn1)Commented:
To create the Import Specification
   1) Click on external data> text file which then opens another window called "Get external data - Text file"
   2) Use radio button to select "Import the source data into a new table in the current database"
   3) specify the source of the file using the browse procedure then click OK
   4) Choose radio button to select delimited format and then click next
   5) this window allows you to choose delimiter and text qualifier and if first row contains names - click next
   6) This window allows you to type the name of the field in the Field Name column, choose data type, and if you want field indexed - also can choose to skip field - then click next
   7) This window allows you to add primary key or designate field as such - click next
   8) Click on ADVANCED button
   9. in the import specification window
   type the name of the field in the Field Name column
   (here you can use the field names of the destination table, specify data type,
   check the box Skip if you do not want to import the column)
   10 click save as, give the specification a name <-- this is the specification name that you will use in the command line below

 DoCmd.TransferText acImportDelim, "ImportSpecificationName", "TableName", "C:\folderName\myText.txt" , True
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

shieldscoAuthor Commented:
Rey - code works for the most part however the files are not being move to an archive folder
Rey Obrero (Capricorn1)Commented:
post the codes that YOU are using.

<code works for the most part> which part is not working?

i can't guessed it if you are not providing detail information.
Rey Obrero (Capricorn1)Commented:
did you define the archiveFolder

dim archiveFolder as string
shieldscoAuthor Commented:
Very Good
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.