Solved

VBA code to write simple Access table to multi-record fixed-width text file with multiple layouts.

Posted on 2009-04-05
8
2,045 Views
Last Modified: 2013-11-27
I have a simple table in Access 2002/2003.  Using VBA, I need to read each record in the table and for each record in the table, write out 3 fixed width text records with defferent layouts to a text file.  So for example, if the table has 10 records, I will end up with a single text file with 30 records, but as I said, each of the 3 text records has a different fixed-width layout. One text record is a Header Record, one record is a Wage Record (empty except for a "W" in column 1) and one record is a Tax Record. Note that not all the input fields are needed and a few constant fields are required on the ouput text file (H, W, T, YN, etc).  The EIN field is NOT a constant.

Setting up the code to loop thru the recordset (Table) was a no-brainer.  I have attached that code.  I have also attached some files showing sample contents of the input recordset and the layouts of the 3 record types.  I created 3 Export File Specifications and tried to use DoCmd.TransferText acExport Fixed, for example:

txtExportName = "C:\Temp\PaymentExport.txt
Docmd.TransferText acExportFixed, RecordHExportSpec, tblFilteredInput, txtExportName
Docmd.TransferText acExportFixed, RecordWExportSpec, tblFilteredInput, txtExportName
Docmd.TransferText acExportFixed, RecordTExportSpec, tblFilteredInput, txtExportName

But using this results in a text file containing only T records (since each TransferText statement overwrites the prior one (and makes the use of the loop irrelevant, anyway)

So how do I make this loop act as follows:

1. Read a single recordset row
2. Append a properly formatted H record to the text file
3. Append a properly formatted W record to the same text file
4. Append a properly formatted T record to the same text file
5. Read the next row of the recordset and repeat steps 2 thru 4
6. Continue as above until EOF

One other small detail -- there is a single $ amount field in the T (Tax) record.  Even using the Export Specification File for the T-record, I can't get the text file to properly fill the amount field with leading zeros and right-justify, for example 000000340.97;  it wants to left-justify the amount and strip off the leading zeros, as: 340.97.
Option Compare Database
Option Explicit
 
Private Sub Main()
 
    Dim db As Database
    Set db = CurrentDb
    Dim recFilteredInput As DAO.Recordset
 
    Set recFilteredInput = db.OpenRecordset("tblFilteredInput", _
        dbOpenDynaset)
 
    Do While Not recFilteredInput.EOF
        'Place here commands to write out text records
                            
        recFilteredInput.MoveNext
    Loop
 
End Sub

Open in new window

tblFilteredInput.jpg
PaymentExport.txt
tblRecordHExportSpec.jpg
tblRecordHKey.txt
tblRecordTExportSpec.jpg
0
Comment
Question by:bbecker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24073334
it is not clear, how the output would look like, but this will get you started
Option Compare Database
Option Explicit
 
Private Sub Main()
 
    Dim db As Database
    Set db = CurrentDb
'    Dim recFilteredInput As DAO.Recordset
    Dim rs As DAO.Recordset
    Set rs = db.OpenRecordset("tblFilteredInput", _
        dbOpenDynaset)
    open "c:\myTextFile.txt" for append as #1
    dim hLine as string,wLine as string, tLine as string
 
    Do While Not recFilteredInput.EOF
       'format the record here
       hLine="H" & rs("GenesisEIN") & string(26," ") & rs("PayrollID") & string(24," ") & rs("LiabilityDate"
        'write the hLine to the textr file
      Print #1,hLine
  
      ' do the same for the   other lines 
        
                            
        recFilteredInput.MoveNext
    Loop
 
End Sub

Open in new window

0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 24073344

Option Compare Database
Option Explicit
 
Private Sub Main()
 
    Dim db As Database
    Set db = CurrentDb
'    Dim recFilteredInput As DAO.Recordset
    Dim rs As DAO.Recordset
    Set rs = db.OpenRecordset("tblFilteredInput", _
        dbOpenDynaset)
    open "c:\myTextFile.txt" for append as #1
    dim hLine as string,wLine as string, tLine as string
 
    Do While Not recFilteredInput.EOF
       'format the record here
       hLine="H" & rs("GenesisEIN") & string(26," ") & rs("PayrollID") & string(24," ") & rs("LiabilityDate"
        'write the hLine to the textr file
      Print #1,hLine
  
      ' do the same for the   other lines 
      wLine=..  
      Print #1,wLine   
 
      tLine=..  
      Print #1,tLine                   
        rs.MoveNext
    Loop
Close #1
End Sub

Open in new window

0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24073485

you can also use instead of this line
open "c:\myTextFile.txt" for append as #1

'the line below
open "c:\myTextFile.txt" for output as #1
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:bbecker
ID: 24073491
Hi capricorn,

Thanks very VERY much.  I see where this is going and I believe it will work.  Do I dare press you for a solution to my additional little problem -- how do I get (for instance) 340.97 to come out on the text file as 000000340.97, or at least properly right-justify the number (with or without the leading zeroes)?  Will I have to parse the field somehow and count the digits then add the appropriate number of zeroes to the left using a variable, or is there a way to right-justify the number?   Thanks again.  You've earned your points already.
0
 

Author Comment

by:bbecker
ID: 24073508
so opening the file as output will clear out any existing data in the file, as opposed to append.  And that will save me the trouble of testing whether the file already exists and KILL it. Right?
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24073509
use format

format(340.97,"000000000.00")
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 24073539
so opening the file as output will clear out any existing data in the file, as opposed to append.  And that will save me the trouble of testing whether the file already exists and KILL it. Right?


Yes
0
 

Author Closing Comment

by:bbecker
ID: 31566834
capricorn1 you are the BEST!!!!!  I owe you my sanity.  Your solution works like a charm.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question