rpg
**FREE
// Read the first record
READ MYFILE;
DOW NOT %EOF(MYFILE);
// Process the record
DSPLY ‘Record read: ‘ + %CHAR(MyRecordField);
// Read the next record
READ MYFILE;
ENDDO;
Random Read with CHAIN:
rpg
**FREE
// Keyed access
CHAIN myKey MYFILE;
IF %FOUND(MYFILE);
DSPLY ‘Record found: ‘ + %CHAR(MyRecordField);
ELSE;
DSPLY ‘Record not found’;
ENDIF;
3. Writing Records to a File
You can write new records to a file using the WRITE operation.
rpg
**FREE
// Set the fields of the record
MyRecordField1 = ‘New Value’;
MyRecordField2 = 123;
// Write the new record
WRITE MYREC MYFILE; // MYREC is the record format name
4. Updating Records in a File
To update an existing record, use the UPDATE operation.
rpg
**FREE
// Read the record first
CHAIN myKey MYFILE;
IF %FOUND(MYFILE);
// Modify the fields
MyRecordField1 = ‘Updated Value’;
MyRecordField2 = 456;
// Update the record
UPDATE MYREC MYFILE;
ENDIF;
5. Deleting Records from a File
To delete a record, use the DELETE operation.
rpg
**FREE
// Read the record first
CHAIN myKey MYFILE;
IF %FOUND(MYFILE);
// Delete the record
DELETE MYREC MYFILE;
ENDIF;
6. Handling Display Files
For display files, use operations like EXFMT to display and get input in a single operation.
rpg
**FREE
// Display the screen and get input
EXFMT MYDSPREC; // MYDSPREC is the record format name
DSPLY ‘You entered: ‘ + %CHAR(MyScreenField);
7. Handling Printer Files
For printer files, use the WRITE operation to send output to the printer.
rpg
**FREE
// Set the fields of the print record
MyPrintField1 = ‘Print Value’;
MyPrintField2 = 789;
// Write the print record
WRITE MYPRTREC MYPRTF; // MYPRTREC is the record format name
These are the basic file manipulation operations in RPGLE. The flexibility of RPGLE allows you to manage files effectively, making it suitable for a wide range of business applications. If you have any specific questions or need more detailed examples, feel free to ask!
09- RPGLE – Subfiles
Subfiles in RPGLE (RPG IV) are a powerful feature that allows you to display and manage lists of data on a screen. They are particularly useful for handling large datasets in a user-friendly manner. Here’s an overview of how to work with subfiles in RPGLE:
Key Concepts
- Subfile: A subfile is a temporary storage area in a display file that holds multiple records, which are displayed on the screen as a list.
- Subfile Control Record: Manages the subfile, including operations like loading, displaying, and clearing the subfile.
- Subfile Record Format: Defines the layout of each record in the subfile.
Steps to Create and Use a Subfile
1. Define the Subfile in the Display File (DSPF)
You need to define the subfile and its control record in the display file. Here’s a simplified example:
plaintext
A R SFLCTLR SFLCTL(SFL)
A SFLSIZ(9999)
A SFLPAG(10)
A SFLDSP
A SFLDSPCTL
A 90 SFLCLR
A 91 SFLEND
A OVERLAY
A R SFL SFL
A FLD1 10A
A FLD2 5Y 0
A FLD3 8Y 2
2. Define the Subfile in the RPGLE Program
In your RPGLE program, you define the subfile and the subfile control records. You load data into the subfile and display it.
rpg
**FREE
// Define the display file
FMYDSPF CF E WORKSTN;
// Declare variables for subfile handling
DCL-S subfileRRN INT(5); // Relative record number
DCL-S recordLoaded IND; // Indicator for record loading
// Load the subfile
subfileRRN = 1;
recordLoaded = *OFF;
// Loop to load records into the subfile
DOU recordLoaded;
// Set subfile fields
FLD1 = ‘Record ‘ + %CHAR(subfileRRN);
FLD2 = subfileRRN * 10;
FLD3 = subfileRRN * 0.75;
// Write to the subfile
WRITESFL SFL;
// Update the relative record number
subfileRRN += 1;
// Stop after loading 10 records (for example)
IF subfileRRN > 10;
recordLoaded = *ON;
ENDIF;
ENDDO;
// Display the subfile
EXFMT SFLCTLR;
*INLR = *ON;
RETURN;
Explanation
- Subfile Control (SFLCTLR): Controls the subfile operations, such as displaying and clearing the subfile.
- Subfile Record Format (SFL): Defines the layout of each record in the subfile, including the fields displayed.
- Loading the Subfile: The loop loads records into the subfile, setting the fields and writing each record to the subfile.
- Displaying the Subfile: The EXFMT operation displays the subfile control record, which in turn displays the subfile records.
Advanced Features
- Paging: You can handle large datasets by loading subfile pages dynamically based on user actions (e.g., scrolling).
- Selection: You can allow users to select records from the subfile for further actions.
- Editing: You can enable users to edit records directly in the subfile and save changes.
Subfiles provide a flexible and powerful way to manage lists of data in RPGLE applications, enhancing user experience and interaction. If you have specific scenarios or need more detailed examples, feel free to ask!
10 – RPGLE – Reports
rpg
**FREE
// Define the printer file
FMYPRTF O E PRINTER OFLIND(*INOF);
// Define a physical file to read data from
FMYFILE IF E DISK ;
DCL-S lineCount INT(10) INZ(0);
DCL-S totalPages INT(10) INZ(1);
// Open the printer file
OPEN MYPRTF;
// Write the header
WRITE HEADER MYPRTF;
// Loop through records in the file
READ MYFILE;
DOW NOT %EOF(MYFILE);
// Set fields for the detail line
FLD1 = myRecordField1;
FLD2 = myRecordField2;
FLD3 = myRecordField3;
// Write the detail line
WRITE DETAIL MYPRTF;
// Increment the line count and check for page overflow
lineCount += 1;
IF lineCount > 50; // Adjust based on your page size
lineCount = 0;
totalPages += 1;
WRITE FOOTER MYPRTF;
SKIP(0001);
ENDIF;
// Read the next record
READ MYFILE;
ENDDO;
// Write the footer
WRITE FOOTER MYPRTF;
// Close the printer file
CLOSE MYPRTF;
*INLR = *ON;
RETURN;
Explanation
- Header Record (HEADER): Printed at the top of each page, typically includes the report title.
- Detail Record (DETAIL): Contains the data for each record from your input file.
- Footer Record (FOOTER): Printed at the bottom of each page or at the end of the report.
Handling Page Breaks
Ensure that your report handles page breaks correctly by checking the line count and printing the header/footer as needed.
Advanced Features
- Grouping and Totals: You can add logic to group records by a certain field and calculate group totals.
- Conditional Formatting: Apply different formats based on the value of the data.
- Multiple Columns: Design your report to display data in multiple columns or tables.
Creating reports in RPGLE allows you to present data in a structured and professional manner, which is essential for business applications. If you need more specific examples or have questions about advanced report features, feel free to ask!