creating a header record

N

notDave

I have a query I am exporting to a txt file and I need to
include a header record.

Any suggestions?

Right now I'm putting my header information in it's own
table and doing a union all with my query to add the two
together. But I have to create bogus columns with null
values in them to match the columns in my query. I also
have to slice the header up into the same field lengths as
my joined query, etc. I've got 11 different headers for
11 different exports and things are getting too messy.
Any advice or direction would be greatly appreciated.

~notDave
 
A

Allen Browne

Another alternative is to create 2 text files (header and data), and then
concatenate them.

Or after you export the data, you could Open the text file for input, Open
another for output, write the header, and line input to write from one to
the other. I've used that approach with files containing a few thousand
lines, and it's pretty quick.
 
N

notDave

I'm not sure I follow your second suggestion. Is there a
way to open a txt file, write a line to the top, then save
the file?

That would be a great solution.

I get the concatenate suggestion, I would have 22 files
(11 data and 11 headers) that I create, and then
concatenate them in sets of two. Each header has specific
differences.


~notDave
 
A

Allen Browne

This kind of thing:

Dim sHeader As String
Dim sPath As String
Dim sFile As String
Dim sFileTemp As String
Dim sTmp As String

sPath = "C:\MyPath\"
sFileTemp = "Tempfile.txt"
sFile "MyFile.txt"
sHeader = "Put your header text into this string."

DoCmd.TransferText acExportDelim, , "Query1", sPath & sFileTemp,
False

Open sPath & sFileTemp For Input As #1
Open sPath & sFile For Output As #2
'Write the header
Print #2, sHeader

'Copy the other file in.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, sTmp
Print #2, sTmp
Loop
Close #2
Close #1

'Delete the temp file.
Kill sPath & sFileTemp
 

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