Expandable Subfile - Example Program

Subfile:

Subfiles are one of the most used concepts on IBM i. 

There are three different types of Subfiles. 
  • Load All Subfile (Click Here to see more about Load All Subfile with an example).
  • Expandable Subfile
  • Single Page Subfile

Message Subfile is used to display messages on the screen. Click Here to see more about Message Subfiles. 

We will see more about Expandable Subfile in this article with an example. Variable has been used to display message on the screen in this example. Message subfile can be used instead.

Expandable Subfile:

As the name suggests Subfile is expanded (loaded) as we press the 'PAGE DOWN' Button. 

Only one page (equal to the number of records mentioned against SFLPAG in DSPF) is loaded on the initial load and the next page is loaded when we press 'PAGE DOWN'. 

Logic for PAGE DOWN needs to be handled in the Program and PAGE UP is taken care by the system (using the records already loaded from Subfile buffer). 

However, Maximum number of records allowed are 9999 similar to Load All Subfile. 

SFLSIZ should be declared with 1 record greater than the number of records declared in SFLPAG Keyword in DDS (SFLSIZ = SFLPAG + 1).

Here is an example with a bit of Options processing using READC & SFLNXTCHG

Physical File:

DDS for Physical file (PPTESTFILE). Any key fields required are to be added as necessary. 

     A          R TESTFILER                          
     A            TFIELD1       10A                  
     A            TFIELD2       10A                  
     A            TFIELD3       10A                  
     A            TFIELD4       10A                  

We can create the Table using below SQL script alternatively.

CREATE TABLE QTEMP/PPTESTFILE 
    (TFIELD1 CHAR (10) NOT NULL WITH DEFAULT, 
     TFIELD2 CHAR (10) NOT NULL WITH DEFAULT, 
     TFIELD3 CHAR (10) NOT NULL WITH DEFAULT, 
     TFIELD4 CHAR (10) NOT NULL WITH DEFAULT)

Display File (DSPF):

DDS for Display file (PPEXPSFLD)

     A* Record Format for Subfile Data                              
     A                                      DSPSIZ(24 80 *DS3)      
     A          R PPSFL01                   SFL                     
     A  70                                  SFLNXTCHG               
     A            SOPT           1A  B 10  4                        
     A            SFIELD1       10A  O 10  9                        
     A            SFIELD2       10A  O 10 28                        
     A            SFIELD3       10A  O 10 48                        
     A            SFIELD4       10A  O 10 66                        
     A* Record Format for Subfile Control                           
     A          R PPCTL01                   SFLCTL(PPSFL01)         
     A                                      CA03(03 'Exit')         
     A                                      PAGEDOWN(41 'Page Down')
     A                                      OVERLAY                 
     A                                      SFLCSRRRN(&CSRRRN)      
     A  25                                  SFLDSP                  
     A  26                                  SFLDSPCTL               
     A  27                                  SFLCLR                  
     A  40                                  SFLEND(*MORE)           
     A                                      SFLSIZ(0009)            
     A                                      SFLPAG(0008)            
     A                                  1  2USER                           
     A                                  1 32'Expandable Subfile'           
     A                                      COLOR(WHT)                     
     A                                  1 73DATE                           
     A                                      EDTCDE(Y)                      
     A                                  2 73TIME                           
     A                                  2 37'Example'                      
     A                                      COLOR(WHT)                     
     A                                  4  3'Select Option, Press Enter...'
     A                                      COLOR(BLU)                     
     A                                  6  3'5=Display'                    
     A                                      COLOR(BLU)                     
     A                                  8  9'Field - 1'                    
     A                                      DSPATR(UL)                     
     A                                      COLOR(WHT)                     
     A                                  8 28'Field - 2'                    
     A                                      DSPATR(UL)                     
     A                                      COLOR(WHT)                     
     A                                  8 48'Field - 3'                    
     A                                      DSPATR(UL)                     
     A                                      COLOR(WHT)       
     A                                  8 66'Field - 4'      
     A                                      DSPATR(UL)       
     A                                      COLOR(WHT)       
     A                                  8  3'Opt'            
     A                                      DSPATR(UL)       
     A                                      COLOR(WHT)       
     A            SRRN           4S 0H      SFLRCDNBR(CURSOR)
     A            CSRRRN         5S 0H                       
     A* Record Format for Footer                             
     A          R PPFTR01                                    
     A                                      OVERLAY          
     A                                 23  2'F3=Exit'        
     A                                      COLOR(BLU)       
     A            MESSAGE       70   O 24  2                 

Program (RPGLE):

Below is the Sample RPGLE program (PPEXPSFLR) written in Full free format RPGLE. 

**Free

  // ------------------------------------------------------------------------

  // Expandable Subfile - Example Program

  // ------------------------------------------------------------------------


  // Input File

  Dcl-F PPTESTFILE Keyed ;


  // Expandable Subfile

  Dcl-F PPEXPSFLD WorkStn Sfile(PPSFL01 : wRRN) ;


  // Stand-Alone Variables Declaration

  Dcl-S wNoOfRecords Packed(1 : 0) ;

  Dcl-S wRrn Packed(4 : 0) ;

  Dcl-S wError Ind ;

  Dcl-S wNoOptions Packed(4 : 0) ;


  // ------------------------------------------------------------------------

  // Main Process

  // ------------------------------------------------------------------------

  ExSr SrInitializeSubfile ;

  ExSr SrLoadSubfile ;

  ExSr SrDisplaySubfile ;

  ExSr SrFinalRoutine ;


  // ------------------------------------------------------------------------

  // Initialize Subfile Routine

  // ------------------------------------------------------------------------

  BegSr SrInitializeSubfile ;


    *In25 = *Off ;

    *In26 = *Off ;

    *In27 = *On ;

    wRrn = 0 ;

    SRrn = 1 ;


    Write PPCTL01 ;


    *In26 = *On ;

    *In27 = *Off ;


    Setll *Start PPTESTFILE ;


  EndSr;


  // ------------------------------------------------------------------------

  // Load Subfile Routine

  // ------------------------------------------------------------------------

  BegSr SrLoadSubfile ;


    wNoOfRecords = 0 ;


    Read PPTESTFILE ;

    Dow Not %Eof(PPTESTFILE)

      And wNoOfRecords < 8

      And wRrn < 9999 ;


      SFIELD1    = TFIELD1    ;

      SFIELD2    = TFIELD2    ;

      SFIELD3    = TFIELD3    ;

      SFIELD4    = TFIELD4    ;


      wRrn += 1 ;

      wNoOfRecords += 1;


      Write PPSFL01 ;


      If wNoOfRecords < 8 ;

        Read PPTESTFILE ;

      EndIf ;


    EndDo;


    wNoOfRecords = 0 ;


    If %Eof(PPTESTFILE) or wRRN = 9999 ;

      *In40 = *On ;

    EndIf;


  EndSr;


  // ------------------------------------------------------------------------

  // Display Subfile Routine

  // ------------------------------------------------------------------------

  BegSr SrDisplaySubfile ;


    Dow *In03 = *Off ;


      If wRrn > 0 ;

        *In25 = *On ;

      EndIf;


      If CsrRrn > 0 ;

        SRrn = CsrRrn ;

      EndIf ;


      Write PPFTR01 ;

      Exfmt PPCTL01 ;


      Select ;

        When *In03 = *On ;

          Leave ;

        When *In41 = *On ;

          ExSr SrLoadSubfile ;

          CsrRrn = 0 ;

          SRrn += 8 ;

        Other ;

          ExSr SrValidateOptions ;

          If wError ;

            MESSAGE = 'Ivalid Options Selected.' ;

          Else ;

            ExSr SrProcessOptions ;

          EndIf ;

      EndSl;


    EndDo;


  EndSr;


  // ------------------------------------------------------------------------

  // Validate Subfile Options

  // ------------------------------------------------------------------------

  BegSr SrValidateOptions ;


    wError = *Off ;


    ReadC PPSFL01 ;

    Dow Not %Eof(PPEXPSFLD) ;


      If SOpt <> '5' And SOpt <> ' ' ;

        wError = *On ;

        *In60 = *On ;

      Else ;

        *In60 = *Off ;

      EndIf;


      *In70 = *On ;

      Update PPSFL01 ;


      ReadC PPSFL01 ;

    EndDo;


  EndSr;


  // ------------------------------------------------------------------------

  // Process Subfile Options

  // ------------------------------------------------------------------------

  BegSr SrProcessOptions ;


    wNoOptions = 0 ;

    ReadC PPSFL01 ;

    Dow Not %Eof(PPEXPSFLD) ;

      If SOpt = '5' ;

        wNoOptions += 1 ;

      EndIf ;

      ReadC PPSFL01 ;

    EndDo ;


    If wNoOptions > 0 ;

      MESSAGE = %Char(wNoOptions) + ' Records Selected.' ;

    EndIf ;


  EndSr;


  // ------------------------------------------------------------------------

  // Final Routine

  // ------------------------------------------------------------------------

  BegSr SrFinalRoutine ;


    *InLr = *On;


  EndSr; 


Comments

Popular posts from this blog

What is the importance of User Open (USROPN) in RPGLE - IBM i

What is Deep Learning? Beyond the Basics of AI

What is Artificial Intelligence?