473,385 Members | 1,838 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,385 software developers and data experts.

Output to comma delimited text file

Avi
I need to create a text file that has the data from the 10 tables in
the database. The number of fields in the tables exceeds 255 and so I
cannot make a new table with all the fields and then export it into a
text file.
Is there any s/w out there I could use? I am not much of a programmer
but I heard I could use VBA to get this done. Any help with the code
will be appreciated.
Thanks

Apr 17 '06 #1
3 7046
your post is not completely clear, but I will give it a shot. I
understand that you want to write data from 10 tables to 1 text file.
What is not clear is if the tables all have 255 fields or not. The
other issue is that data contained in each field. You want a comma
delimited text file. If there is any punctuation in any of the fields,
and that punctuation happens to be a comma, it will throw off your
delimiter (the comma) because you will have 2 commas for that field.
Lets say all the tables have 255 fields and there are no commas in any
of the fields (actually, it doesn't really matter if the tables all have
the same number of fields for writing to a text file - but reading from
the text file to a table might make a difference). Then you can do this
using VBA. The following code assumes you are writing data from an mdb
file:

Sub WriteDataToTextFile()
Dim DB As DAO.Database, RS As DAO.Recordset
Dim str1 As String, strPath As String, arrTbls As Variant
Dim i As Integer, j As Integer

strPath = CurrentProject.Path
arrTbls = Array("tbl1", "tbl2", "tbl3", ..., "tbl10")
Set DB = CurrentDB
Close #1
Open strPath & "\yourTxtFile.txt" For Append As #1
For i = 0 to Ubound(arrTbls)
Set RS = DB.OpenRecordset(arrTbls(i))
Do While Not RS.EOF
str1 = ""
For j = 0 to RS.Fields.Count - 2
str1 = str1 & RS(j) & ","
Next
str1 = str1 & RS(j)
Print #1, str1
RS.MoveNext
Loop
RS.Close
Next
Close #1
End Sub

So what is going on here is that I am placing all the table names in an
array called arrTbls and looping through that array. Then I use DAO
code to loop through the records in each table. The part that writes to
the textfile is

Open strPath & "\yourTxtFile.txt" For Append As #1

and in the Do Loop I collect the values of all the fields in a string
variable called str1 using yet another For Loop, and then I use the
Print function to print that row to the text file, then move on to the
next record.

You can copy/paste this code in a standard code module. Then you can
place your mouse cursor over some of the expressions like Open (from the
Open strpath line) and press the F1 key to bring up a Help file that
explains how the Open statemenet works. You can place your mouse cursor
over most VBA objects (not the variables that you declare) and the
properties or methods and press the F1 key to bring up a Help file that
provides some explanation on how the object works.
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Apr 17 '06 #2
Avi
Thanks for the info Rich.
Heres more info:
1. There are 30-60 fields in each table
2. I made a mistake while posting. We need a tab delimited text file
instead of comma so I guess we dont have to worry about the comma in
the text.
3. I just noticed that I might need to eliminate some of the fields in
the tables. I guess I could update the code you provided and read one
table at a time and eliminate the field using the if statement. (or
maybe use the array number for the table and use it to eliminate the
fields. I guess I will have to eliminate the first 2 fields most of the
time)
4. One quick question. To add a tab between the fields do i use a tab
within quotes (like u did for a comma) or is there any other way to do
it.

Thanks

Apr 17 '06 #3
You might be better off creating a separate text file for each table for
now until you get the hang of what is going on. Here is a modified
version of the first example routine. This routine only does one table.
So all you need to do is to change the name of the table and the name of
the textFile (so that you don't over write the textfile). And for the
delimiter, I changed it from "," to vbTab, a built-in VBA constant for
tab (could also use chr(9))

Sub WriteDataToTextFile()
Dim DB As DAO.Database, RS As DAO.Recordset
Dim str1 As String, strPath As String
Dim i As Integer, j As Integer

strPath = CurrentProject.Path
Set DB = CurrentDB
Set RS = DB.OpenRecordset("tbl1")
Close #1
Open strPath & "\txtFile1.txt" For Append As #1
Do While Not RS.EOF
str1 = ""
For j = 0 to RS.Fields.Count - 2
str1 = str1 & RS(j) & vbTab
Next
str1 = str1 & RS(j)
Print #1, str1
RS.MoveNext
Loop
RS.Close
Close #1
End Sub

You can run this subroutine after your paste it into a Standard Code
module (not a Form code module) by pressing the F5 Key. After you run
the routine, go to windows explorer and check to see if your textfile
exists and contains your table data. Then you can rerun the routine by
changing the Table and textfile names:

Set RS = DB.OpenRecordset("tbl2") '<--change tbl name here
Close #1
'--change textfile name from "\txtFile1.txt" to
'--"\txtFile2.txt" for example
Open strPath & "\txtFile2.txt" For Append As #1

Then rerun the routine
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Apr 17 '06 #4

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

Similar topics

4
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous...
4
by: Christine Forber | last post by:
I wonder if anyone knows of some javascript code to check a comma-delimited list of email addresses for basic formating. What I'm looking for is the javascript code to check a form field on form...
3
by: Elmo Watson | last post by:
I've been asked to develop a semi-automated type situation where we have a database table (sql server) and periodically, there will be a comma delimited file from which we need to import the data,...
1
by: DCM Fan | last post by:
Access 2K, SP3 on Windows 2K, SP4 All, I have an import spec set up with quoted Identifiers and comma-separated values. The text file is produced by a 3rd-party program of which I have no...
1
by: John B. Lorenz | last post by:
I'm attempting to write an input routine that reads from a comma delimited file. I need to read in one record at a time, assign each field to a field array and then continue with my normal...
2
by: Kenneth Koski | last post by:
Hello All, I have a comma delimited text file, which I would like to move into a SQL 2000 table . I created a DTS package in SQL Server and saved it as a VB.bas . I am writting the code in C#...
9
by: Bernie Yaeger | last post by:
Is there a way to convert or copy a .xml file to a comma delimited text file using vb .net? Thanks for any help. Bernie Yaeger
15
by: VMI | last post by:
I'm parsing a comma-delimited record but I want it to do something if some of the string is between "". How can I do this? With the Excel import it does it correct. I'm using String.Split()....
4
by: JustSomeGuy | last post by:
Hi. I have a comma delimited text file that I want to parse. I was going to use fscanf from the C library but as my app is written in C++ I thought I'd use the std io stream library... My Text...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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...

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.