Results 1 to 8 of 8
  1. #1
    a6asics is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    3

    How do I export an Access Table to a non-delimited flat file with pre-defined column width?

    Good Morning,

    I have been tasked with a "proof of concept" assignment. My manager is asking me to take an Access 2016 Table, and export it to a non-delimited, flat file that can be used in mainframe processing. Each output record will be the same length, and there are implied column lengths which must be accommodated for.

    The code must be initiated from a command button within a form inside the application. (The user must not do this on their own using a wizard )Does anyone know of any script that can accomplish that? I have been unsuccessful obtaining this information on the internet, and am hoping someone with programming (vba) knowledge to offer some advice.

    Example: Export table to 100 byte flat file without delimiters:

    Table Name: tstAccessTable


    Column1: Employee_ID (integer) '1234' (Must be output to a 10 byte column, padded to left with zeros)
    Column2: Last_Name (Short String) '"Smith" (Must be 25 characters on output, left justified, padded with spaces)
    Column3: First_Name (Short String) "John" (Must be 15 Characters on output, left justified, padded with spaces)
    Column4: City (Short String) "Anycity" (Must be 15 Characters on output, left justified, padded with spaces)
    Column5: State (Short String) "NY" (Must be 2 Characters on output)
    Column6: Zip_Code_Full (Short String) "12345-6789" (Must be 11 Characters on output, left justified, padded with spaces if there is no zip-4)
    Column7: Department Code (integer) '1234567890' (Must be output to a 10 byte column, padded to left with zeros if the code is less than 10-digits)

    Output record example: 0000001234Smith___________________John___________B uffalo_________NY14201-13241234567890____________

    Remaining fields to the right (pos 89-100 are initializes to spaces).

    Thanks for your help in advance!


    Mark
    Last edited by a6asics; 09-24-2018 at 09:13 AM. Reason: Wrong info in subject line

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    I don't know of any out of the box way to do it but it should be easy enough to code.

    1. Learn how to write to a text file. Here is an example.
    2. Write a function to format a string by padding. Here are some ideas. Here is how to pad numbers.
    3. Write your export sub. You'll need to know how to use a recordset and loop through them in vba.
    ..a. Open text file
    ..b. open recordset
    ..c. loop through recordset, row by row
    ....i. process each field with string padding/formatting functions and save to text file
    ..d. close recordset
    ..e. save text file.
    ..f. close text file
    ..g. profit.

    You could also use your formatting functions in an sql query.

    You mentioned left "justified", did you just mean left aligned?

  3. #3
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,002
    There is a fixed width export wizard option. That should do the trick.
    Click image for larger version. 

Name:	FixedWidth.PNG 
Views:	29 
Size:	160.6 KB 
ID:	35591

    Right click on the table or query and choose Text, and make sure you untick the preserve formatting option.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    a6asics is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    3
    Thanks KD..I will see if I can set this up..thanks for the quick reply!

  5. #5
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    You're welcome! But minty has a much better answer. All that's left for you to do is format your number fields with zero padding, you can do that in a query. Format("1234", "0000000000") => "0000001234"

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    OP explicitly stated users must not interact with wizard.

    Do a one-time export with wizard to create a Specification. Use DoCmd.TransferText referencing saved specification for subsequent exports.

    If necessary, build query that pads values for fixed length and concatenates to one string for each record and reference that query in the export. However, Access drops following spaces, so not sure about how to force spaces in positions 89-100.

    KD's suggestion of VBA writing lines to a text file might be needed.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Peter M is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Dec 2017
    Posts
    67
    Use both the Wizard and VBA to set this up as an XML export to text.

    Step 1) Use the Wizard to help build the export XML
    Step 2) Save the specification
    Step 3) Save the steps
    Step 4) View the XML in VBA and tweak if necessary, if not needed you can make a button that just runs the export job with: currentproject.importexportspecifications(name).ex ecute

    To pad the data fields, depending on how many are needed, build a custom query and make each field a fixed width field by using the STRING command, e.g.:

    Field1_10Byte: string( (10-len([Field1]) ),"0") & [Field1]


    This will add zeros (0) to the front of the Field1 until the length is 10 byte
    e.g. Field1 = "Bob" <- Lenth = 3
    Field1_10Byte = "0000000Bob"

    1)
    Click image for larger version. 

Name:	Export_Fixed_Width_1.jpg 
Views:	21 
Size:	132.1 KB 
ID:	35606

    2)
    Click image for larger version. 

Name:	Export_Fixed_Width_2.jpg 
Views:	20 
Size:	19.9 KB 
ID:	35607

    3)
    Click image for larger version. 

Name:	Export_Fixed_Width_3.jpg 
Views:	21 
Size:	118.1 KB 
ID:	35608

    4)
    Click image for larger version. 

Name:	Export_Fixed_Width_4.jpg 
Views:	21 
Size:	195.0 KB 
ID:	35609

  8. #8
    a6asics is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2018
    Posts
    3
    Thanks everybody!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2018, 11:11 AM
  2. Replies: 5
    Last Post: 06-23-2017, 12:11 AM
  3. Export Access table data to flat file (txt file)
    By edmscan in forum Import/Export Data
    Replies: 3
    Last Post: 06-17-2015, 12:03 PM
  4. Replies: 5
    Last Post: 02-20-2011, 08:22 PM
  5. Importing a tab delimited file into access table - please he
    By championss in forum Import/Export Data
    Replies: 0
    Last Post: 10-29-2006, 02:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums