Discussion Locked

Exporting table with fixed lenght

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I want to export a table called "orders" to a txt file. However each record has to be divided into 3 lines, in each line a few fields have to be put. The text file is used as import file for an ordering system that needs for each order 3 lines, likeheader-line,detail-line and closing-line. Please help me

6 replies

by peyison

Paul,

You'll have to write some VBA code to do this. Something like: (this code is not tested :) )

Dim db as Database
Dim rst as Recordset
Dim sSql as String
Dim iFile as Integer

Set db = DBEngine(0)(0)

sSql = "Select Field1, Field2, Field3, Field4 from tblOrders"

Set rst = db.OpenRecordset(sSql)

( or you could skip the select statement and do:
Set rst = db.OpenRecordset("tblOrders") )

'open the file
iFile = FreeFile

Open "c:\data\orders.txt" for output as iFile

Do Until rst.eof
'write the first line of the orders file
Print #iFile, rst!Field1

'write the second line
Print #iFile, rst!Field2 & "," & rst!Field3

'write the third line - also use a constant and today's date
Print #iFile,rst!Field4 & ",Ship Ground," & Format(Now(), "mm/dd/yy")

'move to the next record
rst.MoveNext

Loop

Close #iFile
-----------------
You could put the code in the Click event of a button on a form.

Hope this helps.

by peyison

Of course you should close down your objects at the end of the code:

rst.close
set rst = nothing
set db = nothing

by bob sellman

A relatively easy way to export in a three line format like you describe is to create a report with no headers or footers and with three lines where each line may have to be a concatenation of the values of interest. You might have to create a blank string of the right length for each line (perhaps with a dummy character at the end) and then insert the values at the right places in each string.

Set the font to Courier or some other fixed pitch font. The report should be "printed" (exported) to a text file format.

If you need a fixed length for each row, you may have to play around with the way you build the text string for each row, but it does work. I've used this method successfully in the past. While it may take some experimenting with the report format (inlcuding page size) for it to work with the destination (the order system), it's pretty easy to set up and play around with the report until you get it right.

This discussion is closed to new replies.