Solved

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

Posted on 2008-10-21
4
662 Views
Last Modified: 2013-11-27
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

Open in new window

0
Comment
Question by:DrEnnui
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
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

by:DrEnnui
ID: 22773000
It is the correct full path and file name.
0
 
LVL 22

Accepted Solution

by:
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

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now