Skip to main content

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

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i