Solved

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

Posted on 2008-10-21
665 Views
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.
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

0
Question by:DrEnnui
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 22

Expert Comment

ID: 22772918
I think sf in this case will only give you the file name, you need a string with the concatenation of f and sf.

Try debugging the code and see what sf is as it runs.

Kelvin
0

Author Comment

ID: 22773000
It is the correct full path and file name.
0

LVL 22

Accepted Solution

Kelvin Sparks earned 125 total points
ID: 22773027
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
0

Author Comment

ID: 22773702
That appeared to do it.  I just had to find a way of doing mass renaming.  Thanks!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…