Example: Program that uses a physical file, display file, and printer file

This example program shows the use of externally described data in a program.

If you enter the DDS for these files on your system and create them using the Create Physical File (CRTPF) command, the Create Display File (CRTDSPF) command, and the Create Printer File (CRTPRTF) command, this program allows you to add records to the physical file, display and update the records, and print a report.

The program is written in Report Program Generator (RPG). You can enter the RPG specifications shown in the example into a source file on your system and create the program using the Create RPG Program (CRTRPGPGM) command.

Figure 1. Example RPG program using a physical, display, and printer file
An illustration of a coding sheet that describes an RPG program that uses a physical, display, and printer file

Legend:

1
These are the file description specifications (F-specs) for the example program. Positions 7 through 14 specify the file names. These file names should be the same file names used when you created the files from the DDS specifications earlier in this appendix.
2
The Es in position 19 indicate that these files are externally described (described in DDS specifications outside the program rather than inside the program).
3
These are the calculation specifications (C-specs).
4
The first section of the C-specs displays a prompt and retrieves a record from the database.

The TAG operation code indicates a label in the program. The EXFMT operation code writes the record PROMPT to the display, and then reads the record when the user presses the Enter key. The CHAIN operation code retrieves a record from CUSMST based on the key field ACTNUM. If no record with that key value is found, indicator 30 is turned on. The program continues to prompt until the key value of an existing record is entered in field ACTNUM (indicator 30 off) or until an A is entered in field ADD.

5
This section adds a new record or updates an existing record in the database file.

If a new customer is being added (indicator 30 is on), the WRITE operation code adds a new record to the physical file. Otherwise, the UPDAT operation code updates an existing record. The program continues to prompt for, retrieve, add, and update records in the physical file until F3 is pressed, setting on indicator 21.

6
This section prints the report.

A record is read from the physical file and the DETAIL record is written to the printer file until the end of the physical file is reached (indicator 45 is set on). The HEADER record is written on the first page and then written again on each new page (indicator 10 is on). When all the records have been written, the CLOSE operation code closes all the files and SETON LR ends the program.