Showcase Article - Subfiles Part 2

Sub-File Processing Part 2

AS/400 subfiles are equivalent to Windows list boxes. Getting comfortable with subfile programming is essential for designing user friendly interfaces. Managers and business analysts need to understand how to use this powerful technique. Using subfiles is an essential skill of the AS/400 programmer.

Remember to implement sub-file processing, you must:

Clear the sub-file
Load the sub-file
Display the sub-file
Process sub-file records selected by the user.

Last month, I showed the code for a user to look up customers that belong to a salesperson. Doing this is similar to running a query to display records in a file. While that is handy, providing the ability to select records from that list is what makes subfile system design so powerful.

With only a few instructions, the program can display more details about the customer using a full screen display. A little more code will allow the user to update fields using that screen. Finally, once the customer has been selected, the program can call another program that uses a subfile to list the open orders for that customer.

First, the user must have a way of indicating which customer to select. There are advanced techniques to select based on the cursor position. However, the easiest technique is to add a one character field to the sub-file. The user simply keys X in the field next to the customer number to select it.

I have added the field, SEL, to the subfile data record. I have also included a heading for it in the subfile control record.

To see which record was selected, I added the subroutine SELRCD after the EXFMT operation. Now, when a user keys an X on a line and hits ENTER, the subroutine can determine which record was selected.

The AS/400 offers a simple and efficient way to read sub-file records. The READC operation reads only records that have been changed. If the user keys a value in the SEL field, the READC operation will read that record. It will automatically skip over any records that were not selected.

So using the READC is an efficient way to look for selected records. Notice that if the user keys spaces in SEL, the AS/400 sees those as changed records. Once the program reads a sub-file data record, all the fields in that line are available to the program. So now, simply process the customer selected.

In Figure 1 you can see the list of customers for salesperson # 6004 with a column for selection. If the user keys X on the same line as Nick’s Deli, the READC operation in the subroutine SELRCD will read that subfile data record. Then, the program can use that information to display more detail about the customer.

Displaying more information about the customer is as simple as: 

    1)  Adding a record in the display file to show more customer fields 
    2)  Adding a File Spec to define the Customer Master File 
        as an input file 
    3)  Doing a CHAIN operation to the customer master file 
        using the CUSNUM field from the subfile data
        record as a key
    4)  Doing an EXFMT operation to display the screen
It is almost as easy to use this interface to update the customer information. Just open the file for update and add an UPDATE operation after the EXFMT. When you’ve done that, the user has a friendly way to select a customer, see details from the customer master and change any information that needs updating.

So what is involved in using this program to call another program to show the open orders for the customer? Use the SELRCD routine to determine which customer was selected. Then, instead of chaining to the customer file, call a new program with CUSNUM as a parameter.

That new program will use the CUSNUM filed to read all open orders for the customer and display them in a subfile list. You could then add similar features to that subfile program to display or change the details of the open orders. You could even let the user select an open order and use that as a parameter to call a program to list the items in the order.

You can see where this is headed. Subfiles give you the ability to design robust, user friendly programs. Combining these techniques with modular programming gives you the ability to build very complicated systems, one simple step at a time.

Figure 1: This is the list of customers for salesperson # 6004.

      Salesperson#  6004 
     Sel   Cust#    Name                               Phone#                          
      _   012795    JOHN'S ALL AMERICAN SPORTS         2105551234                  
      _   021365    AAA TERMITE INSPECTORS             9405556666                  
      _   034598    JONATHANS SPORTS CLUB              2125551212                  
      _   045654    NICKS DELI                         4065551212                  
      _   085261    ZEBRA COMMUNICATIONS               4055551234                  
      _   085265    ANYTOWN INSTALLERS                 9195551256                  
      _   097854    WIDGET CORP                        8175556565                  
      _   124569    GOLD COAST LANDSCAPING             5045558654                  
      _   165432    LAST CHANCE GAS STATION            6455551212                  
      _   232356    TRM TELECOMMUNICATIONS             8885551211                  
      _   456215    NICKS RESTAURANT                   8175559876                  
      _   546832    DANBURG CONSLUTING                 9728176543                  
      _   561245    BOYDS CAR REPAIR                   9625551256                  
      _   654654    CARLS REPAIRS                      8175559963                  
      _   856234    ABC COMPANY                        4055551122
Figure 2: This is the DDS for the display file to select a customer from a list.

    A                                      REF(*LIBL/CUS)            
    A                                      PRINT                     
    A                                      CA03(03)                  
    A          R ENTSLS                                              
     * Let user enter a salesperson # so program can show a list     
     * of customers                                                  
    A                               O  2 20'List Customers for Sls#' 
    A            SLSPER    R        B  2 46EDTCDE(4)                 
    A          R SFDATA                    SFL                       
    A            SEL            1A  I  4  2
A CUSNUM R O 4 5 A CUSNAM R O 4 15 A CUSPHN R O 4 50 *---------------------------------------------------------------- A R SFCTL SFLCTL(SFDATA) A SFLPAG(0015) A SFLSIZ(0045) A OVERLAY A N50 SFLCLR A 50 SFLDSP A 50 SFLDSPCTL A 50 SFLEND(*MORE) A 2 4'Salesperson#' A SLSPER R O 2 17EDTCDE(4) A 3 1'Sel' A 3 5'Cust' A 3 15'Name' A 3 50'Phone#' *---------------------------------------------------------------- A R CMDKEYS A 24 40'F3=Exit' *---------------------------------------------------------------- Figure 3: This is the part of RPG subfile program to process the selected customer. * Define the subfile and the variable that will hold record # (REC#) FCUSD001 CF E WORKSTN F SFILE(SFDATA:REC#) FCUS IF E K DISK * C *IN03 DOWEQ *OFF * Show screen for user to enter the salesperson's number C EXFMT ENTSLS C *IN03 IFEQ *OFF * Clear the subfile C EXSR CLRSF * Load the subfile C EXSR LODSF * Show the command key footer and then the subfile SFCTL C WRITE CMDKEYS C EXFMT SFCTL * Process any seleted records
C ENDIF C ENDDO C MOVE *ON *INLR C RETURN *--------------------------------------------------------------------- C SELRCD BEGSR * Read the changed subfile data records C READC SFDATA 96 C *IN96 DOWEQ *OFF C SEL IFEQ 'X' * Process the customer
* either CHAIN to customer master and EXFMT a display screen
* or CALL a program with CUSNUM as a parameter C ENDIF
C ENDDO C READC SFDATA 96 C ENDSR *---------------------------------------------------------------------

Back to Showcase Articles    |    Back to Main Page    |   Contact Info