I have a module which I loop through and create a comma separated text file. It works for the most part but 1 line of text I write I cannot get in the right spot, it is a trailer record.
Here is my code and after that is a sample table and then my output and then how I would like the output. I cannot get the TRL,MOAA, line to print after each ID, it prints after each SEQ.
Basically it is a trailer record for the ID.
Code
Private Sub BuildFile()
Dim db As Database, rst As Recordset, ID, Item
Dim lngFile As Long
Set db = CurrentDb
Set rst = db.OpenRecordset("SELECT * " & "FROM InputTable ORDER BY ID, Seq", dbOpenDynaset)
'/open text file
lngFile = FreeFile
Open "C:\FileLocation\Testfile.txt" For Output Access Write As #lngFile
rst.MoveFirst
Do Until rst.EOF
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "HDR," & rst![Test] & ",END"
ID = rst!ID
Do While rst!ID = ID
Item = rst!Item
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "DTL," & rst![Seq] & ",END"
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "DTLTX," & rst![Seq] & ",TSQ1" & rst![ID] & ",TLN1,END"
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "DTLTX," & rst![Seq] & ",TSQ2" & rst![MainLine] & ",TLN2,END"
Print #lngFile, "This is ID " & Format(rst!ID, "000000000") & "TRL,MOAA,NDTL" & rst![SeqCount] & ",NMS0,NBS1,NTX" & rst![DetailLines] & ",END"
rst.MoveNext
If rst.EOF Then Exit Do
Do While rst!Item = Item
rst.MoveNext
If rst.EOF Then Exit Do
Loop
If rst.EOF Then Exit Do
Loop
Loop
rst.Close
db.Close
Close lngFile
Set rst = Nothing
End Sub
Table
ID SEQ ITEM MAINLINE TEST SEQCOUNT DETAILLINES
100 1 Item1 John Y 2 2
100 2 Item2 George Y 2 2
101 1 Item5 Deb N 1 2
102 1 Item1 Jewel Y 2 2
102 2 Item3 Lisa Y 2 2
Current Output
This is ID 000000100HDR,Y,END
This is ID 000000100DTL,1,Item1,END
This is ID 000000100DTLTX,1,TSQ1100,TLN1,END
This is ID 000000100DTLTX,1,TSQ2John,TLN2,END
This is ID 000000100TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000100DTL,2,Item2,END
This is ID 000000100DTLTX,2,TSQ1101,TLN1,END
This is ID 000000100DTLTX,2,TSQ2George,TLN1,END
This is ID 000000100TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000101HDR,N,END
This is ID 000000101DTL,1,Item5,END
This is ID 000000101DTLTX,1,TSQ1101,TLN1,END
This is ID 000000101DTLTX,1,TSQ2Deb,TLN2,END
This is ID 000000101TRL,MOAA,NDTL1,NMS0,NBS1,NTX2,END
This is ID 000000102HDR,Y,END
This is ID 000000102DTL,1,Item1,END
This is ID 000000102DTLTX,1,TSQ1102,TLN1,END
This is ID 000000102DTLTX,1,TSQ2Jewel,TLN2,END
This is ID 000000102TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000102DTL,2,Item3,END
This is ID 000000102DTLTX,2,TSQ1102,TLN1,END
This is ID 000000102DTLTX,2,TSQ2Lisa,TLN2,END
This is ID 000000102TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
Expected Output
This is ID 000000100HDR,Y,END
This is ID 000000100DTL,1,Item1,END
This is ID 000000100DTLTX,1,TSQ1100,TLN1,END
This is ID 000000100DTLTX,1,TSQ2John,TLN2,END
This is ID 000000100DTL,2,Item2,END
This is ID 000000100DTLTX,2,TSQ1101,TLN1,END
This is ID 000000100DTLTX,2,TSQ2George,TLN2,END
This is ID 000000100TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END
This is ID 000000101HDR,N,END
This is ID 000000101DTL,1,Item5,END
This is ID 000000101DTLTX,1,TSQ1101,TLN1,END
This is ID 000000101DTLTX,1,TSQ2Deb,TLN2,END
This is ID 000000101TRL,MOAA,NDTL1,NMS0,NBS1,NTX2,END
This is ID 000000102HDR,Y,END
This is ID 000000102DTL,1,Item1,END
This is ID 000000102DTLTX,1,TSQ1102,TLN1,END
This is ID 000000102DTLTX,1,TSQ2Jewel,TLN2,END
This is ID 000000102DTL,2,Item3,END
This is ID 000000102DTLTX,2,TSQ1102,TLN1,END
This is ID 000000102DTLTX,2,TSQ2Lisa,TLN2,END
This is ID 000000102TRL,MOAA,NDTL2,NMS0,NBS1,NTX2,END