473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using VBA to Link a Directory of CSV Files

Hello,

I am attempting to write a procedure that will loop through a directory
of CSV files, linking each of them to a database. I have tested the
following code for XLS files, and it works fine, but receive errors
when I change it to link CSVs.

Sub mcrAllPermut_LinkExternalTables()
Set db = CurrentDb
strDir = "C:\"
strTemp = Dir(strDir & "*.csv*")

Do While Len(strTemp)

strPath = strDir & strTemp

Set tdfLink = db.CreateTableDef(Left(strTemp, 8))

tdfLink.SourceTableName = Left(strTemp, 8)

tdfLink.Connect = "Text;DATABASE=" & strPath

db.TableDefs.Append tdfLink

strTemp = Dir$()
Loop
End Sub

I receive Run-time error '3044': 'C;\<filename>.csv' is not a valid
path.
I have also attempted to manually link one file, then change the
..Connect of that linked table, then .RefreshLink, then .CopyObject to
complete this task, but receive Run-time error '3170': Could not find
installable ISAM.

Here's the code:
Set db = CurrentDb
strDir = "C:\"
strTemp = Dir(strDir & "*.csv*")

Do While Len(strTemp)
'CSV is the name of the table that I linked manually to a .csv file in
the Directory
db.TableDefs!CSV.Connect = strTemp
db.TableDefs!CSV.RefreshLink
DoCmd.CopyObject , Left(strTemp, 8), acTable, "CSV"

Debug.Print strTemp
strTemp = Dir$()
Loop
Your help would be greatly appreciated!
Ben.

Jun 26 '06 #1
3 15711
For anybody interested, I discovered a solution. Syntax failed in my
second approach. The following works:

Sub mcrLinkCSV()
Set db = CurrentDb
strDir = "C:\data\"
strTemp = Dir(strDir & "*.csv*")

Do While Len(strTemp)
strFileName = strTemp
strTblName = Left(strTemp, 8)
Set tdfLink = CurrentDb.CreateTableDef(strTblName)
tdfLink.SourceTableName = strFileName
tdfLink.Connect =
"Text;DSN=CSVLinkSpec;FMT=Delimited;HDR=NO;IMEX=2; CharacterSet=437;DATABASE=C:\data\"
CurrentDb.TableDefs.Append tdfLink
Debug.Print strTemp
strTemp = Dir$()
Loop
End Sub
NOTE: Prior to this, I linked to one of the files using Get External
Data > Link Tables, specified the import parameters, then saved that
import profile (using the Advanced... button) as "CSVLinkSpec".

HTH

Jun 26 '06 #2
Another important NOTE: After I linked to a CSV file manually through
Get External Data > Link Table, I opened MSysObjects to get the
..Connect string
("Text;DSN=CSVLinkSpec;FMT=Delimited;HDR=NO;IMEX=2 ;CharacterSet=437;").

Jun 27 '06 #3
Will
1
I have done similar with Excel files recently. Here's the code: The key is the Set fol = fso.GetFolder(strPath) line.

Setup_Table ("tblX")
Set db = CurrentDb

strPath = "C:\Documents and Settings\wharmon\Desktop\Rapid ILL for Will\Backup05-06\"

' Fill the newly created table above with row and column data from the
' spreadsheet.
' Procedure: Spreadsheets for a year are (or other time frame) are put
' in a single folder. That folder cannot contain any other files.
' The contents of the folder are processed sequentially opening each
' excel file and importing its contents.

Set fol = fso.GetFolder(strPath)

For Each fil In fol.Files
Set ExcelDoc = ExcelApp.Workbooks.Open(fil)

ret = Get_Columns_and_Rows(recCnt, fldCnt, ExcelDoc)

strExcelDoc = fil

Set ExportTable = ExcelDoc.ActiveSheet.Range("A7", "AQ" & recCnt)
Set rst = db.OpenRecordset("tblX")

' For as many rows (records) as exist in the spreadsheet
For intRow = 1 To recCnt
rst.AddNew
rst!fldLender = ExportTable.Cells(intRow, 1)
rst.Update
Next intRow
ExcelApp.Workbooks.Close
Next fil


I'm rarely on these pages, so I am not likely to see your replies. Good luck.
Jul 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: NotGiven | last post by:
I have the following at the beginning of the PHP page: <?php require_once('Connections/conn.php'); ?> I want to hide the connection file, "conn.php", so it's out of the web accessible file...
5
by: Edward Mitchell | last post by:
I am trying to include two class files into a web service project. The structure I have is a top level solution and project in a folder and below that, the web service project in it's own folder. ...
0
by: Johan | last post by:
Hi How do I preform a paged search using the Directory searcher in VB.NET? I'm getting the first 1000 entries in the AD when I'm doing a normal search using the Directory searcher, and then...
1
by: urban.john | last post by:
Here are my steps: create resource files from resx files: <echo message="CREATING RESOURCE FILES FROM RESGEN EN" /> <resgen todir="product\resources_en" verbose="true"> <resources> <include...
13
by: Toby Newman | last post by:
I started learning XML on Monday. I have an XML file and have written an XSL file to render it to HTML for formatted viewing in a browser. I'd like to create a second alternative view of the...
2
by: Gary42103 | last post by:
Hi I need Perl Script to do Data Parsing using existing data files. I have my existing data files in the following directory: Directory Name: workfs/ams Data File Names: 20070504.dat,...
9
by: Julie Smith | last post by:
Hi, Is it just me or does the search pattern parameter in Directory.GetFiles() have a problem with the '?' character? '*.*' works to find all files, but '?.*' does not work to find all files...
3
by: Matt | last post by:
Hey guys. I'm currently working through a number of C programming exercises to get me up to speed with the syntax. Before now I have been using Fortran and I've found Plato3 in Windows to be an...
6
by: kavithadevan | last post by:
Hi, I am trying to retriving some datas from the database.using submit button i am getting result but i inserted one submit image and i gave link to that image and then i run that script but its...
1
by: programmerboy | last post by:
It is been a long time since I have coded something really sleek. And now I am forgetting all the code. I am using VB to try to connect to a directory on another server so I can access its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.