Access 2000 - write a dos-text file

A

A E

I would like to write to a textfile with one or more head rows and then the
records that I want to export. Does anyone have an example code or a
suggestion how to do it. The solution I have in mind also requires a foot
row in the created file. Please reply. /A E
 
D

Douglas J Steele

Something along the lines of the following untested air code should work:

Dim dbCurr As DAO.Database
Dim rsCurr As DAO.Recordset
Dim fldCurr As DAO.Field
Dim intFile As Integer
Dim curTotal As Currency
Dim strFile As String
Dim strOutput As String

strFile = "C:\Output.txt"
intFile = FreeFile()
sngTotal = 0.0

Set dbCurr = CurrentDb()
Set rsCurr = "SELECT * FROM MyTable ORDER BY 1"

If rsCurr.EOF = False
Open strFile For Output As #intFile
' Make a row consisting of the field names,
' separated by commas
For Each fldCurr In rsCurr
strOutput = fldCurr.Name & ","
Next fldCurr
' Write the field name row to the file as a header
' (The Left$(...) removes the extraneous comma from the end)
Print #intFile Left$(strOutput, Len(strOutput) - 1)
Do While rsCurr.EOF = False
curTotal = curTotal + rsCurr!Cost
' Concatenate the values for the row, separating
' them by commas
For Each fldCurr In rsCurr
strOutput = fldCurr & ","
Next fldCurr
' Write the details of the current row to the file
Print #intFile Left$(strOutput, Len(strOutput) - 1)
' Move to the next row in the recordset
rsCurr.MoveNext
Loop
' Write the total cost to the file as a footer
Print #intFile "Total cost = " & curTotal
End If

' Clean up after yourself...
Close #intFile
rsCurr.Close
Set rsCurr = Nothing
Set dbCurr = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top