Combine Three Text Files in Access VBA

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
72,171
Office Version
  1. 365
Platform
  1. Windows
I am using Access VBA to create a "MainFrame"-type file. For those of you who are unfamiliar with "MainFrame" type files, here are the important details:

1. There are three sections, Header (one row at top), Details, and Trailer (one row at bottom)
2. The records are Fixed Width
3. All records must be the same length

The issue is that the record length is often quite long (1500 in my current project), and the format of the Header, Details, and Trailer records are not remotely the same.

So what I have done is I have created three queries, one for each section, and then exported each to a text file, i.e.
tmpHeader
tmpDetails
tmpTrailer

Now, what I would like to do is "combine" these three text files together to make my one, single, complete MainFrame file.

I can do this pretty easy if I create in a one line DOS Batch file (using the COPY command). I also know I could create recordsets in VBA and loop through each, and write out each field, but with the size of each record, that doesn't seem to be the most efficient way to go.

So I am looking for a simple way to do this in Access VBA that is similiar to how I would do it in a DOS batch file, but I can quite figure out how the syntax for. I have been Googling the web, but still haven't found what I am looking for yet.

Does anyone know how to do this?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I don't know if this will work the way you are looking for but you can try looking into the ADODB.Recordset's .GetString method. It will returns the recordset as a string. It's been a while since I've used it but it sounds like it could possibly work. Nothing like a dos command though.

I prefer the FileSystemObject, but that requires looping through the recordset.

hth,

Rich
 
Upvote 0
Thanks Rich.

I am not familiar with that Method, so I don't quite know how it would work.

I am playing around with some code found here:
http://groups.google.com/group/micr...ramming/browse_thread/thread/17039bd4f6a8563b

I got it to work temporarily in some basic testing, but have now stumbled on some "File Not Found" errors when trying to incorporate it into my project. I guess I need to play around with it a little more.
 
Upvote 0
OK, I got that code to work out.

I am running this through an AutoExec macro, so first I have a Function which takes the variables I am inputting and passes them along to my Procedures. It also checks to make sure that the file path I am sending has a backslash on the end:
Code:
Public Function MyCombineFilesFunc(myFileDir As String, myHeader As String, myDetails As String, myTrailer As String, myFinalFile As String)
'   Calls combine files procedure
 
    Dim myFileDirExp As String
 
'   Check for missing backslash on end of file dir
    If Right(myFileDir, 1) <> "\" Then
        myFileDirExp = myFileDir & "\"
    Else
        myFileDirExp = myFileDir
    End If
 
'   Call procedure to combine files
    Call MyCombineFiles(myFileDirExp, myHeader, myDetails, myTrailer, myFinalFile)
 
End Function
Here is the procedure that I manipulated from the prior link:
Code:
Public Sub MyCombineFiles(myFileDir, myHeader, myDetails, myTrailer, myFinalFile)
'   Combines designated files in a given file path (in the same path) and deletes the originals
'   Code from Jim Rech, [URL]http://groups.google.com/group/microsoft.public.excel.programming/browse_thread/thread/17039bd4f6a8563b[/URL]
 
'   myFileDir = location where files to combine reside
'   myHeader = name of Header file to combine
'   myDetails = name of Detail file to combine
'   myTrailer = name of Trailer file to combine
'   myFinalFile = name of final combine files
 
    Dim SrcFiles
    Dim CurrSrc As String
    Dim DestFile As String
    Dim Counter As Integer
    Dim TextLine As String
 
'   Combine files
    SrcFiles = Array(myFileDir & myHeader, myFileDir & myDetails, myFileDir & myTrailer)
    Open myFileDir & myFinalFile For Output As #1
    For Counter = 0 To UBound(SrcFiles)
        Open SrcFiles(Counter) For Input As #2
        Do While Not EOF(2)
            Line Input #2, TextLine
            Print #1, TextLine
        Loop
        Close #2
    Next
    Close #1
 
'   Delete temp files
    Kill myFileDir & myHeader
    Kill myFileDir & myDetails
    Kill myFileDir & myTrailer
 
End Sub
 
Upvote 0
Using the code I borrowed previously, I was able to modify this a bit to work for another situation. The following code Access VBA code will merge all text files into a single text file, and delete the original files that were included in the merge (you can elect to not delete them by commenting out the "Kill" line):
Code:
Public Sub MyCombineFiles(myFileDir As String, myFileExt As String, myFinalFileName As String)
'   This process will combine all text files in a directory into one single file (should only be used with text files)
'   myFileDir = location where files to combine reside (including final backslash)
'   myFileExt = file extension of files you want to copy (including period)
'   myFinalFileName = file name and extension of final combined file

    Dim fname
    Dim TextLine As String
    Dim myCombinedFile As String

'   Create output file
    myCombinedFile = myFileDir & myFinalFileName
    Open myCombinedFile For Output As #1
    
'   Loop through all files with designated extension in the directory
    fname = Dir(myFileDir & "*" & myFileExt)
    While (fname <> "") And (fname <> myFinalFileName)
'   Open file and write data to output file
        Open myFileDir & fname For Input As #2
        Do While Not EOF(2)
            Line Input #2, TextLine
            Print #1, TextLine
        Loop
        Close #2
'   Delete initial file after it is merged (comment this line out if you do not want to delete initial files)
        Kill myFileDir & fname
'   Go to the next file
        fname = Dir()
    Wend
    Close #1

End Sub
So, you would call it to run by feeding it arguments, something like this:
Call MyCombineFiles("C:\Temp\",".txt","Combined.txt")
 
Upvote 0
Joe:
When creating a 'main frame' file you can omit the header and trailer if desired. The file specifications can be given in the JCL (Job Control Language) which describes the format of the file. The length (lrecl) and blocksize (blksize) are stated in the data control block (DCB) You simply include the bypass label processing (BLP) option.Main frame files may also be of variable length.

Jack
 
Upvote 0
When creating a 'main frame' file you can omit the header and trailer if desired.
Jack, we were not loading Main Frame files, we were creating them for a very specific exact Main Frame-type format that we were required to meet ("as is").
Those options your describe may be true in general for loading Main Frame files, but they were not made available to us.

In any event, that part (the original question) was resolved a few years ago. My recent post of yesterday actually has nothing to do with Main Frame type file formats. It is just a variation of my code that uses Access VBA to merge any number of text files into a single text file (the original code just merged exactly three text files).

It wasn't a question, I just posted it for the benefit of others who may wish to do the same thing.
 
Upvote 0

Forum statistics

Threads
1,214,624
Messages
6,120,591
Members
448,973
Latest member
ksonnia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top