Processing multiple files Please use speaker notes for additional information! When you are processing multiple files you can choose to process horizontally which is what I did in the last slide show and in the initial examples. Or you can process vertically which means you can loop through each file until it changes identification number - this means until it no longer matches the identification number in the hold area. This example shows the vertical processing involving three files - the processing for two files is similar and shown in code.
This shows the results of executing the program. File1mul.dat 111011 111001 111010 222002 333003 333033 444004 555055 600006 666600 666060 666006 File2mul.dat 111001 111010 222022 222222 300003 444004 444040 444444 555006 666006 666060 777007 File3mul.dat 111110 111011 112012 222022 333033 333303 444404 666606 666060 This slide shows the three input files and the results. The processing is to combine the three files into one. The logic flowchart is shown on the next slide and the code in VB follows.
START Open files ID1 > ID2 ID2 > ID3 ID1> ID3 ID2 to hold This shows the start which includes establishing the hold area with the lowest id from the three files being processed.
Process with id from hold ID1 = hold Add to accum Read 1 Routine ID2 = hold Add to accum Read 2 Routine ID3 = hold Add to accum Read 3 Routine Process with id from hold and accum - Write The processing is done until there is a 999 in all three files. It could also be set to stop when all 3 files are at end of file. A
A ID1 > ID2 ID2 > ID3 ID1> ID3 ID2 to hold ID3 to hold Establish the smallest id for the hold area. 0 to accum
Option Explicit Dim Data1 As String, Data2 As String, Data3 As String Dim ID1 As String, ID2 As String, ID3 As String Dim AMT1 As Integer, AMT2 As Integer, AMT3 As Integer Dim ID123 As String, AMT123 As Integer Dim wkAccum As Integer Dim wkIdHold As String Private Sub cmdExit_Click() Close #1 Close #2 Close #3 Close #4 End End Sub This starts an example coded in VB. It shows the definition of the work areas/variables and the closing of the files when the program is exited.
Private Sub cmdRun_Click() Do Until (ID1 = "999" And ID2 = "999" And ID3 = "999") Do While ID1 = wkIdHold wkAccum = wkAccum + AMT1 Call ReadFile1 Loop Do While ID2 = wkIdHold wkAccum = wkAccum + AMT2 Call ReadFile2 Do While ID3 = wkIdHold wkAccum = wkAccum + AMT3 Call ReadFile3 ID123 = wkIdHold AMT123 = wkAccum Call Process If ID1 > ID2 Then If ID2 > ID3 Then wkIdHold = ID3 Else wkIdHold = ID2 End If If ID1 > ID3 Then wkIdHold = ID1 wkAccum = 0 End Sub Loop through each file comparing and processing those that match the hold id . Again this is processing each file vertically as opposed to the horizontal processing covered before. This is the code to loop through the records processing each one. Note that if you are doing any kind of special processing and dealing with errors, lots needs to be added to handle those instances.
Private Sub Form_Load() 'This program combines three files into one ’All files can have one or no or many records Dim DBPath As String DBPath = App.Path If Right(DBPath, 1) <> "\" Then DBPath = DBPath & "\" End If Open DBPath & "File1mul.dat" For Input As #1 Open DBPath & "File2mul.dat" For Input As #2 Open DBPath & "File3mul.dat" For Input As #3 Open DBPath & "file123m.dat" For Output As #4 Call ReadFile1 Call ReadFile2 Call ReadFile3 If ID1 > ID2 Then If ID2 > ID3 Then wkIdHold = ID3 Else wkIdHold = ID2 If ID1 > ID3 Then wkIdHold = ID1 End Sub This determines what to initialize wkIdHold at. The lowest id from the three files will be used to initialize.
Private Sub ReadFile1() If Not EOF(1) Then Input #1, Data1 ID1 = Left(Data1, 3) AMT1 = Right(Data1, 3) Else ID1 = "999" End If End Sub Private Sub ReadFile2() If Not EOF(2) Then Input #2, Data2 ID2 = Left(Data2, 3) AMT2 = Right(Data2, 3) ID2 = "999" Private Sub ReadFile3() If Not EOF(3) Then Input #3, Data3 ID3 = Left(Data3, 3) AMT3 = Right(Data3, 3) ID3 = "999" Private Sub Process() picOut123.Print ID123; AMT123 Write #4, ID123 & AMT123 This shows the read and check for EOF and the processing which writes the record containing the id that came from the hold area and the amount that came from the accumulator.