Export table in to multipe text files

W

wcprod

I need some help with some code to export a table into multiple tab delimited
text files. Each text file would have the field headers and be based on an
export specification. Ideally it would create text files in the same format
as the transfertext function does. In the end I want to have x number of tab-
delimited text files (export(1).txt, export(2).txt .....) each with 1000
records until every record in the table has been exported. Any help would be
appreciated...
 
G

Graham Mandeno

Hi wcprod

It sounds like you already have an export specification that will export the
entire table. I think, then, that the easiest solution would be to do that
into a temporary file and then open the resulting file and write 1000
records into each of the final files until the temp file is exhausted.

Here is some very rough "air code":

Dim iRecord as Long, iFile as integer
Dim sHeader as string, sRecord as string
Dim hInFile as integer, hOutFile as integer

' Use TransferText to create temp file

hInFile = FreeFile
hOutFile = FreeFile
Open <path to temp file> for input as #hInFile
Line Input #hInFile, sHeader
Do Until EOF(hInFile)
If iRecord mod 1000 = 0 then
if iFile > 0 then Close #hOutFile
iFile = Int(iRecord / 1000) + 1
Open <folder path> & "\export(" & iFile & ").txt" for output as
#hOutFile
Print #hOutFile, sHeader
End If
Line Input #hInFile, sRecord
Print #hOutFile, sRecord
iRecord = iRecord + 1
Loop
Close #hOutFile
Close #hInFile
Kill <path to temp file>
 

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