Lesson 180 of 288
Printer & Report ProgrammingMini Project: Batch Report Program
A printer file report program listing every customer balance with a grand total, designed to run as a batch job.
What We Are Building
A printer file report program, RPT001R, listing every customer's
balance with a grand total, designed from the start to run as a batch
job rather than interactively.
Business Scenario
Imagine an accounting team needing a printed customer balance report every night, without anyone needing to sit and watch it run. This project builds exactly that report, then the CLLE Wrapper to Run a Report mini project, next in this batch, covers actually submitting it.
Concepts Used
- Report record formats and the heading-then-detail-loop pattern, from the Report Record Formats and Report Headings and Detail Lines lessons.
- Page overflow handling, from the Page Overflow Basics lesson.
- Report totals, from the Report Totals and Summary Lines lesson.
- Native
READand%EOF, from the RPGLE File I/O lessons.
Files and Programs Involved
CUSTMAST, read natively from beginning to end.RPT001P, a new printer file with three record formats:HDGFMT(heading),DTLFMT(detail), andTOTFMT(total).RPT001R, the RPGLE program that produces the report.
Step-by-Step Build Outline
- Design
RPT001P's three record formats:HDGFMTwith a title and column headings,DTLFMTwith customer number, name, and balance, andTOTFMTwith a single grand total field. - Declare
RPT001PandCUSTMASTinRPT001Rwithdcl-f. - Write
HDGFMTonce, before the read loop begins. - Loop with native
READand%EOFoverCUSTMAST, checking the overflow indicator before each detail line and accumulating a running total, exactly as covered in the Report Totals and Summary Lines lesson. - Write
TOTFMTonce, after the loop ends.
Example Code
dcl-f RPT001P printer;
dcl-f CUSTMAST disk;
dcl-s balTotal packed(11:2) inz(0);
write HDGFMT;
read CUSTMAST;
dow not %eof(CUSTMAST);
if *inof;
write HDGFMT;
endif;
balTotal += CUSTBAL;
write DTLFMT;
read CUSTMAST;
enddo;
write TOTFMT;
*inlr = *on;
This is the same combined heading, overflow, and total pattern covered individually across the Printer Files / Reports lesson group, now brought together into one complete report program.
How to Test It
Run the program interactively first, using WRKSPLF to view the
resulting spool file directly, confirming the heading appears once,
every customer's detail line appears, and the total at the end matches a
manually calculated sum for a small, known set of test customers.
Common Mistakes
- Writing
HDGFMTinside the read loop instead of before it, printing it once per customer instead of once per page. - Writing
TOTFMTinside the loop instead of after it, printing a partial, still-accumulating total. - Forgetting the overflow indicator check, leaving later pages of a long report without column headings.
Debugging Checklist
If the total looks wrong, follow the Report Totals and Summary Lines
lesson's guidance: confirm with EVAL that balTotal starts at zero and
is increased by exactly CUSTBAL on each pass, using a breakpoint set on
the write DTLFMT; line.
Possible Extensions
A natural extension is adding a control break, printing a page break between customers grouped by region, if such a field existed, using the deliberate page-break pattern covered in the Controlling Page Breaks lesson.
Quick Recap
RPT001Rcombines heading-once, detail-per-row, overflow handling, and a final total into one complete report program.HDGFMTwrites once before the loop;TOTFMTwrites once after it.- Testing against a small, known set of customers lets you verify the total by hand.
Try Asking the AI Tutor
Use the AI Tutor to practice extending or troubleshooting this project. For example, try asking:
- "How would I add a count of customers alongside the balance total on this report?"
- "What would this report look like on a very long customer list if the overflow check were left out entirely?"