Wednesday, July 8, 2020

Make second line appear and disappear on a subfile

subfile that expands to a 2nd line and contracts

I was asked how to do this by someone who said that no-one in his department knew how to: have a second record appear in a subfile when a function key is pressed, and disappear when the function key was pressed again.

Fortunately this is a simple thing to do with just three keywords in the display file's DDS:


And there is not much we have to do in the RPG program that uses the subfile.

I have deliberately stripped down the display file and RPG code to what I consider a minimum to, in my opinion, makes it easier to understand.

Let me jump right in and show you the display file I will be using in this example:

01 A                                      INDARA
02 A                                      CA03(03 'F3=Exit')
03 A          R SFL01                     SFL
04 A                                  3  3'First line'
05 A            Z1RRN          3S 0O  3 15
06 A                                  4  5'Second line'
07 A          R CTL01                     SFLCTL(SFL01)
08 A                                      SFLSIZ(0100)
09 A                                      SFLPAG(0010)
10 A                                      SFLDSP
11 A                                      SFLDSPCTL
12 A  30                                  SFLEND(*MORE)
13 A  31                                  SFLDROP(CA11)
14 A N31                                  SFLFOLD(CA11)
15 A                                      SFLMODE(&MODE)
16 A            MODE           1A  H
17 A                                  1  2'TEST DISPLAY FILE'

This display file consists of three parts:

  1. Header level keywords, lines 1 and 2
  2. Subfile record format, lines 3 – 6
  3. Subfile control record format, lines 7 – 17

Line 1: The INDARA means that I using the indicator area in the display file and the indicator data structure in my RPG program. That way I can give the indicators real names, rather than them just being anonymous numbers.

Line 2: I will be using indicator 3 to exit the program.

Lines 3 – 5: I think this is the simplest subfile record format I have ever created. Notice that lines 4 and 5 will display on the same subfile line, while line 6 will display on the following line.

Line 7: This line tells the display that this is the subfile control record format for the previous subfile record format.

Lines 8 and 9: This is a "load all" subfile. The subfile can contain a maximum of 100 records, and will display a maximum of 10 on each page.

Lines 10 – 11: as I know this subfile is going to contain data I don't need to condition these keywords with indicators.

Line 12: I prefer "More..." and "Bottom" at the bottom of my subfiles. To use these I have condition the keyword with an indicator.

Line 13: SFLDROP: This displays just the first line of the subfile records. When the given function key, F11, is pressed it open up and display the second line of the subfile records.

Line 14: SFLFOLD: This displays the second line of the record. When the given function key is pressed the second line of the subfile is not displayed.

Lines 15 and 16: SFLMOD and the hidden variable MODE allows me to retrieve which mode, "expanded" or "contracted", the subfile is in.

Line 17: Display heading.

Now onto the RPG program:

01  **free
02  ctl-opt option(*nodebugio:*srcstmt:*nounref)
              dftactgrp(*no) ;

03  dcl-f TESTDSPF workstn indds(Dspf)
                             sfile(SFL01:Z1RRN) ;

04  dcl-ds Dspf qualified ;
05    Exit ind pos(3) ;
06    SflMore ind pos(30) ;
07    SflMode ind pos(31) ;
08  end-ds ;

09  LoadSubfile() ;
10  Dspf.SflMode = '1' ; //=1 lines
11  Dspf.SflMode = '0' ; //=2 lines
12  Mode = Dspf.SflMode ;

13  dow (1 = 1) ;
14    exfmt CTL01 ;

15    if (Dspf.Exit) ;
16      leave ;
17    endif ;

18    Dspf.SflMode = Mode ;
19  enddo ;

20  *inlr = *on ;

Line 1: If the RPG ain't free it ain't my code.

Line 2: These are my favorite control options I add to all my RPG programs.

Line 3: This is the file definition for my display file. The INDDS gives the name of the indicator data structure. SFILE tells the program which field is the "key", relative record number, for the subfile I will be using.

Lines 4 - 8: This is the indicator data structure. I will be using just three indicators from the display file, and these are the name I will be calling them in this program.

Line 9: This subprocedure is where I load the subfile. I will show the code for it later in this post.

Lines 10 and 11: These lines are here for testing purposes only. In the real world you would use line 10 or 11, not both.

Line 12: Here I set the field Mode so that the right variation of the subfile is displayed.

Lines 13 – 19: I am using a Do loop within which I am displaying the subfile, including control record.

Line 18: The Mode field in the display file is updated with which version of the subfile is shown. I take that value and update the indicator that controls either SFLDROP or SFLFOLD version of the subfile.

I said I would show the subprocedure that loaded the subfile, and here it is:

21  dcl-proc LoadSubfile ;
22    for Z1RRN = 1 to 30 ;
23      write SFL01 ;
24    endfor ;

25    Dspf.SflMore = *on ;
26  end-proc ;

Lines 22 – 24: I am using a For group to write 30 records to the subfile.

Line 25: I need to set on this indicator so that I will see More… and Bottom at the bottom of my subfile screens.

So what do I see when I call this program?


  First line  001
    Second line
  First line  002
    Second line
  First line  003
    Second line
  First line  004
    Second line
  First line  005
    Second line
  First line  006
    Second line
  First line  007
    Second line
  First line  008
    Second line
  First line  009
    Second line
  First line  010
    Second line

As the mode is zero both lines of the subfile record is displayed.

Now I press F11 and the subfile collapses, removing the second lines from the subfile records.


  First line  001
  First line  002
  First line  003
  First line  004
  First line  005
  First line  006
  First line  007
  First line  008
  First line  009
  First line  010
  First line  011
  First line  012
  First line  013
  First line  014
  First line  015
  First line  016
  First line  017
  First line  018
  First line  019
  First line  020

I can press F11 to toggle between the two variants to my heart's content, and page up and down to see the rest of the subfile's records. Yes, the code is as easy as that.

You can learn more about this from the IBM website:


This article was written for IBM i 7.4, and should work for some earlier releases too.

No comments:

Post a Comment

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.