It's time to create a simple load-all subfile using the IBM RPG programming language on the IBM i Power System. This code is backwards compatible to the older iSeries machines, and then ancient AS400 systems.
In this example, we’ll display a list of customers on the screen in alphabetical order. The user can search for a customer by entering their name or partial name. Once the list is displayed, the user can select a customer to view more details.
Here are the steps to create this basic subfile:
- Define the Display File (DSPF):
- Create a display file with two record formats: one for the subfile (SFL) and another for the control record (CTL).
- Define the subfile record format (SFL) with the necessary fields (e.g., customer name, ID, etc.).
- Specify the page size, record number, and display mode for the subfile.
- Design the Subfile Layout:
- In the subfile record format (SFL), add headings, colors, and fields from the physical file (e.g., customer data).
- Define any indicators needed for display, end, and clear functions.
- Write the RPGLE Program:
- Declare the physical file (containing customer data) and the display file.
- Define a variable for the record number of the subfile.
- Write a subroutine to clear the subfile (switch on the clear indicator and write the control record).
- Write a subroutine to load the subfile (read the physical file and write the subfile record format for each record).
- In the main routine, call the clear and load subroutines, then display the subfile (switch on the display indicator and write the control record).
- Example RPGLE Code: Below is a simplified example of an RPGLE program for a load-all subfile. This is not a working RPG code example, instead it is an example of the type of code you will write for a typical RPG SUBFILE program. You’ll need to adapt this code to your specific requirements and file structures:
** The DSPF is "subfile" with two record formats: SFL001 (the subfile) and CTL001 (the control format)
F Customer IF E K Disk
F Subfile CF E Workstn
D NumRecords S 5 0
D SubfileRecNo S 5 0
C *Entry PLIST
C Eval SubfileRecNo = 1
C CallP LoadSubfile
C CallP DisplaySubfile
C *InLR Seton
C LoadSubfile Begsr
C Read Customer
C Dow Not %Eof(Customer)
C Eval SubfileRecNo += 1
C Write SFL001
C Read Customer
C Enddo
C Endsr
C DisplaySubfile Begsr
C Eval *In50 = *off // SFLCLR Indicator
C Write CTL001
C Eval *In50 = *on // SFLDSP/SFLDSPCTL Indicator
C Exfmt CTL001
C Endsr
What does this code example mean?
This is a classic RPG program that uses a subfile to display records from a customer file on a display screen. I'll explain each section clearly:
File Declarations (F-Specs)
F Subfile CF E Workstn
- Customer: A physical file (database table) named "Customer" is defined as input (IF), externally described (E), with keyed access (K), stored on disk (Disk).
- Subfile: A display file (DSPF) named "Subfile" is defined as a combined file (CF) for both input and output, externally described (E), and tied to a workstation (Workstn)—typically a terminal screen.
The display file "Subfile" has two record formats:
- SFL001: The subfile record format (holds the repeating rows of data).
- CTL001: The control record format (manages the subfile display, e.g., headers or function keys).
Data Declarations (D-Specs)
D SubfileRecNo S 5 0
- NumRecords: A standalone numeric variable, 5 digits, no decimals (not used in this code snippet).
- SubfileRecNo: A standalone numeric variable, 5 digits, no decimals, used to track the subfile record number.
Main Procedure (C-Specs: Mainline)
C Eval SubfileRecNo = 1
C CallP LoadSubfile
C CallP DisplaySubfile
C *InLR Seton
- *Entry PLIST: Defines the program's entry point (though no parameters are specified here).
- Eval SubfileRecNo = 1: Initializes the subfile record number to 1.
- CallP LoadSubfile: Calls the LoadSubfile subroutine to populate the subfile with data from the Customer file.
- CallP DisplaySubfile: Calls the DisplaySubfile subroutine to show the subfile on the screen.
- *InLR Seton: Sets the Last Record indicator (*InLR) to on, signaling the program to end after execution.
Subroutine: LoadSubfile
C Read Customer
C Dow Not %Eof(Customer)
C Eval SubfileRecNo += 1
C Write SFL001
C Read Customer
C Enddo
C Endsr
- Begsr: Begins the LoadSubfile subroutine.
- Read Customer: Reads the first record from the Customer file.
- Dow Not %Eof(Customer): A "Do While" loop that continues as long as the end-of-file (%Eof) is not reached.
- Eval SubfileRecNo += 1: Increments the subfile record number.
- Write SFL001: Writes the current Customer record's data to the subfile record format (SFL001).
- Read Customer: Reads the next record from the Customer file.
- Enddo: Ends the loop.
- Endsr: Ends the subroutine.
Purpose: This subroutine loads all records from the Customer file into the subfile (SFL001), assigning each a sequential record number.
Subroutine: DisplaySubfile
C Eval *In50 = *off // SFLCLR Indicator
C Write CTL001
C Eval *In50 = *on // SFLDSP/SFLDSPCTL Indicator
C Exfmt CTL001
C Endsr
- Begsr: Begins the DisplaySubfile subroutine.
- Eval *In50 = *off: Turns off indicator *In50, which is typically tied to the SFLCLR (subfile clear) keyword in the DSPF. This clears the subfile of any prior data.
- Write CTL001: Writes the control record format (CTL001) to the display, preparing it for the subfile data.
- Eval *In50 = *on: Turns on indicator *In50, which is typically tied to SFLDSP (subfile display) and SFLDSPCTL (subfile display control) keywords in the DSPF. This makes the subfile visible.
- Exfmt CTL001: Displays the control record format (CTL001) and waits for user input (e.g., pressing Enter or a function key).
- Endsr: Ends the subroutine.
Purpose: This subroutine manages the display of the subfile on the screen, clearing it first, then showing the loaded data.
Program Flow Summary
The program starts and initializes SubfileRecNo to 1.
It calls LoadSubfile, which:
- Reads each record from the Customer file.
- Writes it to the subfile (SFL001), incrementing SubfileRecNo for each record.
It calls DisplaySubfile, which:
- Clears the subfile (via *In50 = *off and Write CTL001).
- Displays the subfile with its control format (via *In50 = *on and Exfmt CTL001).
The program ends (*InLR Seton).
What Does This Program Do?
This is a simple RPG program that:
- Reads all records from a Customer file (e.g., a database table with customer data like name, ID, etc.).
- Loads them into a subfile (a scrollable list on the screen).
- Displays the subfile to the user via a display file (Subfile), allowing them to view the list.
Assumptions
The Customer file has fields (e.g., customer ID, name) that match the fields defined in the SFL001 record format in the DSPF.
The DSPF (Subfile) is properly designed with:
- SFL001 as the subfile format.
- CTL001 as the control format, with indicators like SFLCLR (clear subfile) and SFLDSP/SFLDSPCTL (display subfile) tied to *In50.
Notes
Let me know if you’d like further clarification or help modernizing this code!