?
Solved

Export Table to Text via VBA

Posted on 2003-02-27
6
Medium Priority
?
1,124 Views
Last Modified: 2008-03-31
The goal of my project is to read in a dbf file, and after some processing, output it as a text file with either comma-delimiting, tab-delimiting, or fixed-width. Given the dbf files can have various field widths and number of fields. What is the best way using VBA to programmatically export the table? I have looked at the TransferText method, but it requires a schema file and I cannot find (through Help or Google) any details on how to build such a file.

Thank you for your help and be kind as this is my first question.
0
Comment
Question by:Moliere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Expert Comment

by:funke
ID: 8035404
You can create a schema file by using the File --> Get External Data --> Import menu options.  Use the wizard to import a template file and during that process you can create a schema.  On the first page of the wizard choose the Advanced button on the lower left and you'll get an interface to create a schema... on the right of this screen you do a save as...

Another way is to write directly to a file while looping through a recordset - this will do the same and you don't have to worry about schemas.
0
 
LVL 2

Expert Comment

by:i014354
ID: 8035499
For delimited text files and Microsoft Word mail merge data files, you can leave this argument blank to select the default import/export specifications.

DoCmd.TransferText acExportDelim, , _
    "ExternalReport", "C:\temp\ExternalReport.txt"
0
 
LVL 5

Expert Comment

by:funke
ID: 8035532
just a note - you don't really need to import a file to create it - you just need to get access to start the import wizard so you can create the file - so pick any text file (remember to change the Files of Type dropdown from .mdb to All Files) and this will get you started.

0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 2

Expert Comment

by:kraig
ID: 8035565
To use code to create the file:

Public Function CreateTextFile()

 Dim myFile As String
 Dim strFileName As String

 Dim intFile As Integer

 strFileName = "yourfilename"

 myFile = "C:/YourDirectory")
 
 myFile = myFile & "/" & strFileName & ".txt"
 
 intFile = FreeFile
 
 Open myFile For Output As intFile
 Print #intFile, "Text to write to file, line 1."
 Print #intFile, "Text to write to file, line 2."
 
 Close intFile
 
End Function
0
 
LVL 2

Accepted Solution

by:
kraig earned 300 total points
ID: 8035619
To write the table to the file:

 Dim rs As Recordset
 Dim fld As Field

 Dim strLine as String
 
 Set rs = DBEngine(0)(0).OpenRecordset("YourTable", dbOpenForwardOnly)
 
 Do While Not rs.EOF
   
    For Each fld In rs.Fields
        strLine = strLine & rs(fld.Name) & ", "
    Next

    strLine = Left(strLine, Len(strLine) - 2)
   
    Print #intFile, strLine
   
    strLine = ""
   
    rs.MoveNext

 Loop
 
 rs.Close
 Set rs = Nothing
0
 
LVL 3

Author Comment

by:Moliere
ID: 8036937
This code worked except that I had to declare rs and fld as variants. Also, I like the way I can designate the split character as a variable instead of ", ".

Thanks for your help.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Suggested Courses

771 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