Question : import multiple CSV files in a single directory to an Access table

I have a large collection of CSV files in a single folder that I want to append to a single table in Access 2007, and have decided that the builtin VBA would be the easiest way to do so, regardless of my inexperience with it.

While testing the code snippet below, it would "Run time error 3011" on the DoCmd.TransferText line, claiming that the object 'filename.txt' - correctly the first file in the directory - could not be found.  The variable holding the filepath "sf" shows the correct path value while debugging.  I am suspecting that the format of the filename, which includes periods and spaces, might be a problem.  The format of the filename is determined by the exporting application and is beyond my control.

If the name format is my problem, how can I replace the offending characters programatically?  If the name is not the problem, what am I doing wrong.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Dim fso As New FileSystemObject
Dim f As Folder
Dim sf As File
Dim path As String
 
path = "C:\path\to\directory\"
Set f = fso.GetFolder(path)
 
For Each sf In f.Files
    DoCmd.TransferText acImportDelim, , "tbl_TempBuys", sf, -1
Next
Open in New Window Select All

Answer : import multiple CSV files in a single directory to an Access table

Then it is as you suspect the nameing of files. You may have to look at some code to rename these files into acceptable format prior to importing. I have struck similar with Excel files names and developed code to remove all spaces and other characters before using.

Kelvin
Random Solutions  
 
programming4us programming4us