A Subfile Type for Every Occasion, Part 1: Load-All vs. Self-Extending Subfiles

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

There are three types of subfiles: load-all, self-extending, and page-at-a-time. A load-all subfile loads all the records at one time, before the subfile is displayed. What makes it a load-all subfile is that IBM i handles the paging for you. There's no code in your RPG program to allow you to page up or down through your subfile. You have only one routine, which loads data from your data file until either the end-of-file is reached or the maximum number of allowed records is reached.

By Kevin Vandever

Editor's Note: This article is excerpted from Chapter 2 of Subfiles in Free-Format RPG: Rules, Examples, Techniques, and Other Cool Stuff, Second Edition, by Kevin Vandever (MC Press, 2011). Part 1 compares load-all and self-extending subfiles and adds a position-to field. Part 2 covers page-at-a-time subfiles, a more readable way to handle function keys, and guidelines for choosing the right subfile type.

The book was written when RPG offered free-format calculations but still required fixed-format F and D specs, so the code shows the /Free directive and fixed-format declarations. The subfile techniques haven't changed. Where today's syntax differs, we've added notes like this one.

This type of subfile is very easy to code and can allow you to get a working application up and running rather quickly. It's great if you're certain about the number of records you want to display. Because the OS handles the paging for you, you can just load and display. The user can page through the data, and you don't even code for it in your RPG.

However, if something seems too good to be true, it probably is. The problem with load-all subfiles comes when the amount of data increases. If there are more records than the subfile was built to hold, the program will not blow up, but the extra data will not display. You could set the SFLSIZ keyword as high as 9,999 to load more. But what about the poor users who have to scroll through seemingly endless pages of data to find what they're looking for? Scrolling through several pages of data can be time-consuming, especially if the desired data is close to the bottom of the list.

One last pitfall to the load-all technique is the potential performance implications incurred when so much data is loaded at one time. Not only does it take some time to load thousands of records to a subfile, but the task also requires a considerable amount of system resources. You have to balance how many records you want to display, how many potential pages you want the user to page through, and the potential performance issues of loading the data involved.

Let's say you are not sure how many records will be displayed, or you know the number, but it's relatively high (for example, 1,000 records). How do you best write a subfile program that displays all the records, but also allows the user easy access? You do this by using the second of the three subfile types: self-extending subfiles.

Self-Extending Subfiles

A self-extending subfile can't hold more than 9,999 records either, so in that respect it works the same as a load-all subfile. However, it differs in a couple of other ways. A self-extending subfile allows the user to add records to the subfile only when they are needed. Normally, this is done one page at a time, although it doesn't have to be.

By loading only a specified amount of data at a time, the self-extending subfile's performance will be more consistent than a load-all subfile, which might load 50 records one time and 500 records the next. Another advantage is that by using a self-extending subfile and a position-to technique, you can make navigation within the subfile more flexible.

Let's look at the DDS and the RPG program for this technique. (The complete DDS and RPG for the programs used here can be downloaded from the book's page at mc-store.com. I'll be showing you only the parts of the code that are pertinent.) Figure 1 shows the subfile control record format from the display file, SFL002DF. It's the only part of the DDS that differs from a load-all subfile.

     A          R SF1CTL                    SFLCTL(SFL1)

     A*

     A                                      SFLSIZ(0016)

     A                                      SFLPAG(0015)

     A                                      OVERLAY

     A                                      ROLLUP(27)

     A N32                                  SFLDSP

     A N31                                  SFLDSPCTL

     A  31                                  SFLCLR

     A  90                                  SFLEND(*MORE)

     A            RRN1           4S 0H      SFLRCDNBR

     A                                  6  2'Last Name'

     A                                      DSPATR(HI)

     A                                  6 26'First Name'

     A                                      DSPATR(HI)

     A                                  6 50'MI'

     A                                      DSPATR(HI)

     A                                  6 55'Nick Name'

     A                                      DSPATR(HI)

     A                                  1  2'SFL002RG'

     A                                  1 71DATE

     A                                      EDTCDE(Y)

     A                                  2 71TIME

     A                                  1 24'Subfile Program with Position To'

     A                                      DSPATR(HI)

Figure 1: The DDS subfile control record format for a self-extending subfile (SFL002DF).

Notice that SFLSIZ (16) is just one greater than SFLPAG (15). Both load-all and self-extending subfiles require that SFLSIZ be greater than SFLPAG. Where it really matters is in how you load the subfile in your RPG code. I could have used 500 here, too, but my standard for self-extending subfiles is to make SFLSIZ one greater than SFLPAG and only load one page of records at a time in my RPG program.

Next, notice that I added the ROLLUP keyword. This will allow my RPG program to control when more records are added to the subfile. With a self-extending subfile, it's important to note that the duties of paging through the data will be shared by your RPG program and the OS. Adding the ROLLUP keyword tells IBM i that whenever the last page of the subfile is displayed and the user presses the Page Down key, the program will take control and handle the paging. When the user isn't on the last page, or is paging back through the data, IBM i will handle the paging.

Now let's glance at the RPG program (SFL002RG). Figure 2 shows the F and D specs for the program.

     FSfl002df  CF   E             Workstn

     F                                     Sfile(Sfl1:Rrn1)

     FSfl001lf  IF   E           K Disk

     DSflpag           C                   Const(15)

     DLstrrn           S              4  0 Inz(0)

     Di                S              4  0 Inz(0)

Figure 2: The standard F and D specs for a subfile program (SFL002RG).

Notice that the specs include a stand-alone field called LSTRRN. This field will be used to contain the last relative record number written to the subfile during the subfile load routine. When the user presses the Page Down key, the program will know where in the subfile to start writing records.

Editor's Note: In fully free-form RPG, which is available on every currently supported IBM i release, these declarations use DCL-F, DCL-S, and DCL-C instead of F and D specs. Figure 2 would look like this:

**free

dcl-f SFL002DF workstn sfile(SFL1 : RRN1);

dcl-f SFL001LF keyed;

dcl-c SFLPAG 15;

dcl-s LSTRRN packed(4 : 0) inz(0);

dcl-s i      packed(4 : 0) inz(0);

The source no longer needs /Free and /End-Free directives, and the code can start in column 1.

Notice also that I have separated the clearing of the subfile from the loading of the subfile by placing the tasks in two separate subroutines. I do this in a self-extending subfile program because I don't want to clear the subfile every time I load records. Because I'm going to add records to the subfile when the user presses the Page Down key, I don't want to clear it first. Figure 3 shows the clear subfile routine.

       Begsr Clear_Subfile;

         Rrn1 = *Zero;      // Clear the subfile record number.

         Lstrrn = *Zero;    // Clear the saved subfile record number.

         *In31 = *On;       // Set the indicator to clear the subfile.

         Write Sf1ctl;      // Clear the subfile.

         *In31 = *Off;      // Turn OFF the clear subfile indicator.

         *In32 = *Off;      // Set the indicator to display the subfile.

         *In90 = *Off;      // Turn off the subfile end indicator.

       Endsr;

Figure 3: The clear routine for a self-extending subfile (SFL002RG).

In the Clear_Subfile subroutine, I set both the relative record number (RRN1) and the last relative record number (LSTRRN) to zero. After setting on indicator 31, which conditions the SFLCLR keyword, I write to the subfile control record format to clear the subfile. Then I set off indicator 31, which conditions the SFLDSPCTL keyword, and set off indicator 32, which conditions the SFLDSP keyword. Lastly, I set off the SFLEND indicator, 90.

The subfile load routine shown in Figure 4 is a little different from the one for a load-all subfile. This is mainly because I'm only going to load one page of data at a time, not all the available records from my database file. In this case, field SFLPAG is set as a constant in my D specs and will contain the same number as the SFLPAG keyword in DDS. If you decide to change the keyword in the DDS, you'll have to change the D spec in your RPG. Don't let the fact that they have the same name fool you. I just like doing it that way.

       Begsr Build_Subfile;

         Rrn1 = Lstrrn;         // Set the current relative record number

                                // equal to the last saved relative record

                                // number so that the subfile will load

                                // correctly.

         For i = 1 to Sflpag;   // Load subfile with one page of data.

           Read Sfl001lf;

           If %eof;             // If end of file is reached,

             *In90 = *On;       // set subfile end indicator on.

             Leave;             // Leave the For loop.

           Endif;

           Rrn1 = Rrn1 + 1;     // Increment the subfile record number.

           Write Sfl1;          // Write the data record to the subfile.

         Endfor;

         If Rrn1 = *Zero;       // If no records were written to the subfile,

           *In32 = *On;         // turn on the indicator to NOT display the

         Endif;                 // subfile.

         Lstrrn = Rrn1;         // Save the last record number for the next

                                // page build.

       Endsr;

      /End-Free

Figure 4: The load routine for a self-extending subfile (SFL002RG).

If end-of-file is reached, indicator 90 will come on, and the routine will exit the FOR loop. Indicator 90 also conditions SFLEND. Otherwise, the routine increments the relative record number by one and writes to the subfile record format. Once the loop has finished its work by either writing the prescribed number of records to the subfile (15, in this case) or hitting end-of-file, it checks to determine whether to display the subfile by conditioning indicator 32 and then sets LSTRRN appropriately.

Now, let's look at the main routine, shown in Figure 5.

      /Free

       // Main Routine

       Exsr Clear_Subfile;   // Execute the subfile clear routine.

       Exsr Build_Subfile;   // Execute the subfile build routine.

       Dou *Inkc or *Inkl;   // Process until F3 or F12 is pressed.

         Write Fkey1;        // Display the function key line.

         Exfmt Sf1ctl;       // Display the subfile.

         Select;             // Process data entered by the user.

           When *In27 And (Not *In32);

             Exsr Build_Subfile;   // Execute the subfile build routine.

         Endsl;

       Enddo;

       *Inlr = *On;

Figure 5: The mainline routine for a self-extending subfile (SFL002RG).

This isn't very complex. I've added code to handle the Roll Up key to the original load-all mainline. Remember that I used 27 with the ROLLUP keyword in the DDS, which means that when the Page Down (Roll Up) key is pressed, indicator 27 will be set on. If indicator 27 is on and the subfile isn't empty (*IN32 is off), the code simply executes the Build_Subfile subroutine, which will add to the subfile from where it left off, without clearing it. (The terms page down and roll up are synonymous, as are page up and roll down.)

Let's go back to the EXFMT command for a minute. Remember that with a self-extending subfile, paging duties are shared between your program and the OS. This is where that decision takes place. Your program will sit on this line of code until a valid function key (as defined in your DDS) or the Enter key is pressed. However, when the Page Down key is pressed, IBM i is smart enough to know whether it's about to page through data already in the subfile, in which case it can handle the operation. If the last page of the subfile is already displayed, IBM i knows to pass control back to your program.

IBM i always handles the page-up duties in a self-extending subfile. When you call your program for the first time, the first page of data is displayed. If you press Page Down, control passes back to your program, and 15 more records are written to the subfile, which now holds 30 records, with records 16 through 30 on the screen. If you press Page Up to get back to the first page, or Page Down again to return to the second, IBM i handles it, because those records already exist in the subfile. Only when you ask for a third page is control passed to your program to add 15 new records.

The self-extending subfile is probably the most widely used type of subfile. It balances nicely between letting IBM i handle a bit of the processing and allowing you to add some code to increase flexibility for the user.

Editor's Note: The Clear_Subfile and Build_Subfile routines are written as subroutines (BEGSR/EXSR). They work as is on any current release. Many shops now write them as subprocedures (DCL-PROC) and call them with clearSubfile(); instead of Exsr Clear_Subfile;, which lets you declare variables locally. The 9,999-record maximum for SFLSIZ still applies in current releases of IBM i.

Add Some Oomph: The Position-To Field

Now that you have the self-extending basics down, let's look at a technique to make your subfile programs more flexible for the user.

One thing I like to do in my subfile programs is give the user an additional navigation tool. It could get tedious, for instance, if a user is interested in a name beginning with "V," and the file contains thousands of names listed in alphabetical order. It would be nice if the user could position to the names starting with "V," and page from there.

Better yet, if the user knows the exact name, he could position to that location without paging at all. The position-to field allows that flexibility. It isn't required, but I always use it in the subfile control record format whenever I create a subfile. To me, writing a subfile program without position-to capabilities is like being forced to stop on every floor of a high-rise building, even if you live on the top floor.

The position-to field enables the user to navigate directly to a specific position in a subfile. For example, suppose the "A"s in a name list are displayed. You could enter a name, or partial name, starting with "Z" in the position-to field, and the subfile would position to the "Z"s, or the name closest to "Z" if there were none in the file.

Let's take another look at the subfile control format from SFL002DF. This time, I've added a position-to field called PTNAME. Figure 6 shows the pumped-up control record format.

     A          R SF1CTL                    SFLCTL(SFL1)

     A*

     A                                      SFLSIZ(0016)

     A                                      SFLPAG(0015)

     A                                      OVERLAY

     A                                      ROLLUP

     A N32                                  SFLDSP

     A N31                                  SFLDSPCTL

     A  31                                  SFLCLR

     A  90                                  SFLEND(*MORE)

     A            RRN1           4S 0H      SFLRCDNBR

     A                                  6  2'Last Name'

     A                                      DSPATR(HI)

     A                                  6 26'First Name'

     A                                      DSPATR(HI)

     A                                  6 50'MI'

     A                                      DSPATR(HI)

     A                                  6 55'Nick Name'

     A                                      DSPATR(HI)

     A                                  1  2'SFL002RG1'

     A                                  1 71DATE

     A                                      EDTCDE(Y)

     A                                  2 71TIME

     A                                  1 24'Subfile Program with Position To'

     A                                      DSPATR(HI)

     A                                  4  2'Position to Last Name . . .'

     A            PTNAME        20   B  4 30CHECK(LC)

Figure 6: The subfile control format with the position-to field PTNAME (SFL002DF).

Setting SFLPAG to 15, rather than filling the screen with records, makes room for the position-to field, PTNAME, on line 4 of the screen. In keeping with my standard for self-extending subfiles, SFLSIZ is one greater, at 16. I define it as a 20-byte field, which matches the field length for the LAST NAME field in the data file. The "B" in position 38 allows the user to make an entry in the field. The CHECK keyword with the LC parameter allows lowercase characters.

In the RPG program, the only additional logic necessary is a WHEN block in the mainline routine to determine whether something is entered in the position-to field. If something is entered, the program needs to do the following:

1. Clear the subfile by executing the Clear_Subfile routine.

2. Position the data file with a SETLL operation using the PTNAME field.

3. Load the subfile by calling the Build_Subfile routine.

4. Clear the PTNAME field before displaying the subfile to the screen.

Editor's Note: Figures 6 and 7 come from the finished version of the program, which uses a more readable way to tell which key was pressed. The ROLLUP keyword no longer has indicator 27, and the code compares CFKEY (a one-byte key code from the display file's file information data structure, which the program adds with an INFDS keyword) with a constant named ENTER. Kevin explains this technique in Part 2. For now, read the WHEN in Figure 7 as "when the user presses Enter and has typed something in the position-to field."

Figure 7 shows an example of the block of code you might use for this. No matter where you are in your subfile, the subfile will be cleared when you enter something in the PTNAME field, and 15 new records will be written and displayed, based on what was entered.

           When (Cfkey = Enter) And (Ptname <> *Blanks);

             Setll (Ptname) Sfl001lf;

             Exsr Clear_Subfile;   // Execute the subfile clear routine.

             Exsr Build_Subfile;   // Execute the subfile build routine.

             Clear Ptname;         // Clear the position-to field.

Figure 7: The mainline logic to handle the position-to field (SFL002RG).

Figures 8 and 9 show what happens when the position-to field is used. Figure 8 shows the file beginning with the last name "Anthony." To position the file to "Vandever," simply type that into the position-to field and press Enter. The list will be repositioned, as shown in Figure 9.

A Subfile Type for Every Occasion, Part 1: Load-All vs. Self-Extending Subfiles - Figure 8 

Figure 8: A subfile example involving a list of names.

A Subfile Type for Every Occasion, Part 1: Load-All vs. Self-Extending Subfiles - Figure 9

Figure 9: The list of names repositioned to the name "Vandever."

Editor's Note: CHECK(LC) lets users type mixed case, but SETLL compares exactly what was typed. If last names are stored in uppercase, a user who types "vandever" may land in the wrong place. One fix is to convert the entry before positioning, for example setll (%upper(PTNAME)) SFL001LF;. The %UPPER built-in function is available on IBM i 7.5 and 7.6.

Coming in Part 2

A self-extending subfile still stops at 9,999 records, and once you use the position-to field, the only way back to earlier records is to position again. In Part 2, Kevin covers the page-at-a-time subfile, which removes both limits. He also replaces cryptic indicators such as *INKC and *IN27 with readable key names, and offers guidelines for choosing the right subfile type for the job.

Kevin Vandever

Kevin Vandever began his information technology career in 1984. Once a young, promising RPG programmer and consultant, he cranked out code, spoke at conferences, wrote articles, penned columns, and published the occasional book. Then, he moved into management. Now Kevin reviews code, approves conferences, and occasionally reads articles, columns, and books. His days are filled with words and phrases like “budgets,” “status,” “deadlines,” and “resource constraints” and no longer with technically charged words and phrases such as “loop,” “pointer,” “compile,” and “Who needs to test?” But no matter how far into management he’s gone, Kevin still finds time to interject into every single technical discussion—no matter what the technology, language, or system—the point that the issue could probably be solved using subfiles in RPG. Kevin blogs at kevinvandever.com and can be followed on Twitter at @kevinvandever.


MC Press books written by Kevin Vandever available now on the MC Press Bookstore.

Subfiles in Free-Format RPG Subfiles in Free-Format RPG
Become a subfile master with the concepts, styles, examples, and advanced techniques you’ll find in this book.
List Price $79.95

Now On Sale

LATEST COMMENTS

Buyer's Guide Search

Popular Products

Nexus Portal
44,903
IPCharge
39,471
IPCharge
39,471
Barcode400
38,166
WebSmart ILE and PHP
37,790
Presto
37,382
Catapult
36,308
Catapult
36,308
EDI Software - EZConnect iSeries EDI/XML Software Solutions
26,230
EDI Software - EZConnect iSeries EDI/XML Software Solutions
26,230

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: