Exporting to a txt-file with fixed field length

P

Petterq

I need to make a report that i can export to a txt-file, and where each field
in the report have to start at a specific position on a line, independent of
what has been put into the previous fields.

For instance the name-field for a customer has to take up 50 characters,
even if the name of the customer is shorter.

How can i achieve this?
 
G

Graham Mandeno

Hi Petterq

The following function will transform a value into a fixed width string,
truncating or padding with spaces as required:

Public Function FixString( v as Variant, w as Integer) as String
FixString = Left(v & Space(w), w)
End Function

You can now write some code to open your recordset and, for each record,
append the fixed length value of each field to a string, then write that
string to a text file.

Note that none of this involves a report - just a query and a bit of VBA
code.
 
P

Petterq

Hi.

The code might work, but I am afraid that I have no idea where to put it.

I have a basic query, that lists out the fields I want. How can I include
the code into the process?

Petterq

Graham Mandeno said:
Hi Petterq

The following function will transform a value into a fixed width string,
truncating or padding with spaces as required:

Public Function FixString( v as Variant, w as Integer) as String
FixString = Left(v & Space(w), w)
End Function

You can now write some code to open your recordset and, for each record,
append the fixed length value of each field to a string, then write that
string to a text file.

Note that none of this involves a report - just a query and a bit of VBA
code.
--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

Petterq said:
I need to make a report that i can export to a txt-file, and where each
field
in the report have to start at a specific position on a line, independent
of
what has been put into the previous fields.

For instance the name-field for a customer has to take up 50 characters,
even if the name of the customer is shorter.

How can i achieve this?
 

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