Link to home
Start Free TrialLog in
Avatar of Bill
BillFlag for United States of America

asked on

Export a table to a fixed width text file using a macro in Access 2016

I have an Access 2016 database with a table and query that I want to export to fixed width text files monthly (I give a data example below). The problem is that I can't guarantee that the access database structure will remain the same. Due to this I can't use the built in saved exports feature in access. I have copied and modified a script that will create a schema.ini for me on the fly with the proper schema formatting, including field names, field widths, etc. The problem is I am uncertain how to use it to export the data. I was reading this rather informative web page about how to work with a schema.ini file but it references how to use it for import, not export:


I was originally trying to use the TransferText method but I just keep running into errors with it. Most sites I am reading that are newer seem to recommend using ADO commands now.
DoCmd.TransferText acExportFixed, "schema.ini", tableName, "C:\TestFolder\TestFile.txt", True

I am willing to use any method that works and am open to suggestions.

An example of my issue is that in one month it would be that the database table has 2 fields, both width 10:
fieldID Integer
fieldUserName Char Width 10

Open in new window


But the next month it could have 3 fields and different sizes:
fieldID Integer
fieldUserFName Char Width 25
fieldUserLName Char Width 25

Open in new window


I have a script that can create a schema file on the fly to account for the structure changes but I am not sure how to export the data using the updated schema file.
[TestFile.txt]
ColNameHeader = True
CharacterSet = ANSI
Format=FixedLength
Col1=fieldID Text Integer 10
Col2=fieldUserFName Text Width 25
Col3=fieldUserLName Text Width 25

Open in new window

Avatar of Norie
Norie

What errors were you getting with TransferText?
Probably a more "solid" example will prove helpful to everybody.
I have never used a schema file to perform a fixed width export.

Generally, I just open a text file and loop through each record and each field and write the data to the text file, making sure to pad (left or right) the data in each fielddepending on your data format.

check out Open #1 in the help Access for the syntax for opening and closing the text file an then Write #1 for how to write to the file.
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill

ASKER

PatHartman, I think the MSys tables will do what I need. I am working on it now and will reply to everyone if it does not work.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Using the export functionality of Access it is a bit limited...and in your case is more trouble
Follow the advise from Dale ....you would just iterate the data and write the data based on the Fields count..
Some simple "rules" on what "size" you want and that's all
Avatar of Bill

ASKER

What I ended up doing for now was using a saved exports and TransferText. I am then using the MSys tables to verify the table structure has not changed by comparing them to the table or query I am exporting. Because I am doing this as a side project and don't have time right now if it doesn't match I will just have them notify me and I will manually fix the Saved Export, but my ultimate goal will be to set it up to automatically modify the MSys table if they do change the database without me needing to intervene.

Thank you everyone for your advice on this.