473,799 Members | 2,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Li nkExternalTable s()
Set db = CurrentDb
strDir = "C:\"
strTemp = Dir(strDir & "*.csv*")

Do While Len(strTemp)

strPath = strDir & strTemp

Set tdfLink = db.CreateTableD ef(Left(strTemp , 8))

tdfLink.SourceT ableName = Left(strTemp, 8)

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

db.TableDefs.Ap pend 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!CS V.Connect = strTemp
db.TableDefs!CS V.RefreshLink
DoCmd.CopyObjec t , Left(strTemp, 8), acTable, "CSV"

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

Jun 26 '06 #1
3 15790
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.Creat eTableDef(strTb lName)
tdfLink.SourceT ableName = strFileName
tdfLink.Connect =
"Text;DSN=CSVLi nkSpec;FMT=Deli mited;HDR=NO;IM EX=2;CharacterS et=437;DATABASE =C:\data\"
CurrentDb.Table Defs.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 "CSVLinkSpe c".

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=CSVL inkSpec;FMT=Del imited;HDR=NO;I MEX=2;Character Set=437;").

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

Setup_Table ("tblX")
Set db = CurrentDb

strPath = "C:\Documen ts and Settings\wharmo n\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(s trPath)

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

ret = Get_Columns_and _Rows(recCnt, fldCnt, ExcelDoc)

strExcelDoc = fil

Set ExportTable = ExcelDoc.Active Sheet.Range("A7 ", "AQ" & recCnt)
Set rst = db.OpenRecordse t("tblX")

' For as many rows (records) as exist in the spreadsheet
For intRow = 1 To recCnt
rst.AddNew
rst!fldLender = ExportTable.Cel ls(intRow, 1)
rst.Update
Next intRow
ExcelApp.Workbo oks.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
11024
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 structure. I understand putting them in the php include directory shoudl do this. For example, let's say the directory I put them in is /www/phpIN/, and /www/ht/ is the root web directory. So I put "conn.php" in the inlcude directory...
5
13474
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. In the top level folder I have two class files, classA.cs and classB.cs. In the web service project, I use Add>Existing Item... and browse to select the files in the parent directory. Unfortunately, when I hit the Open button in the file...
0
1108
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 what??? How do I get the rest? I'we understod that need to do a paged search but how do I do that?
1
3359
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 name="${AppName}\**.en.resx" /> <include name="${AppName}\Global.asax.resx" /> </resources>
13
9633
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 same data. I am confused how: If the following line, which dictates which XSL file should be used to transform my XML document for me, is found *in the XML file itself* then how can you use more than one XSL to present multiple views of the same...
2
2436
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, 20070503.dat, 20070502.dat In each of above data files there will be some millions of records. So my job is read those data files and also read first 3 letters of each record in all above data files and write into new data files.For example
9
4635
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 that have only a one letter (and infinite extension size) filename. In fact, it seems to act the same as '*.*'... Is this by design?
3
1979
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 excellent environment in which to create, compile, debug and run my Fortran programmes. I was delighted when I found that Plato3 can also work in C. However, I am at the stage where I need to be using multiple source files and to be linking their...
6
1536
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 not working now.Can u tell me using link how to retrive the datas. here i pasted my script ---------------------search.php-------------- <form name="form1" method="get"> <table width="200" border="1">
1
1170
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 data/files. Can any one please let me know which referrence I need to include? Also, it will be great if you can provide a small code snippet of connecting to a directory on a different server (I know UNC path will be used) and getting its data like how many...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.