473,407 Members | 2,326 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,407 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 15713
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.