13
Wed, Nov
5 New Articles

Modifying a Subfile: Change Is Good, Part 2

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

We started some good stuff in Part 1. Now, let's look at more.

 

Editor's Note: This article is an excerpt from the book Subfiles in Free-Format RPG.

 

In Part 1, I introduced a technique for modifying data files using subfiles, showing you the DDS and sample screen to let a user update, add, and delete from a name file. In Part 2, we move on to the RPG code.

 

Figure 1 shows the RPG program F specs that work with the DDS. Notice that I have defined two subfiles in the F specs for display file SFL004DF. These two subfiles correspond to the two subfiles defined in the DDS.

 

 

FSfl004df cf   e             Workstn

 

 

 

F                                     Sfile(Sfl1:Rrn1)

 

 

 

F                                     Sfile(Sfl2:Rrn2)

 

 

 

F                                     Infds(Info)

 

Figure 1: F specs for the display file in RPG program SFL004RG

 

When the user presses the Enter key in the main routine with nothing in the position-to field, the Process_Subfile subroutine is executed. Figure 2 shows this code snippet. The subroutine then reads the changed subfile records and processes them accordingly.

 

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

 

Exsr Process_Subfile; // Process options taken on the subfile.

  

Figure 2: Calling the Process_Subfile subroutine when the user presses Enter with nothing in the position-to field

 

Let's look at the Process_Subfile subroutine, shown in Figure 3. This routine contains a DO loop that reads the changed records in SFL1. When a user enters one of the valid options in the options field, including a blank (which can be done with the spacebar or field-exit key), the READC operation picks up that record and runs it through the select routine.

 

 

Begsr Process_Subfile;

 

Exsr Clear_Subfile_2; // Clear the confirmation subfile.

 

Readc Sfl1;           // Read all changed records in the subfile.

 

Dow Not %eof;         // Do while there are changed records.

 

Select;

 

Figure 3: Subroutine Process_Subfile, which processes the user options

 

Depending on which option was selected, the program will execute another subroutine (or, in the case of Option 4, write records to another subfile). Selecting options 2 and 5 will show the user a detail screen of data related to that subfile record. In this example, the detail screen will contain the address information of the name listed on the subfile record.

 

It is important to remember that users can select multiple records for processing. That is, a user can choose more than one record to be displayed, changed, or deleted. He or she can also select a mixture of the options to be processed. For example, suppose the user wanted to view subfile records 1, 3, and 5, delete records 9 and 10, and change address information on record 7. He or she would simply type the appropriate option next to each of the appropriate subfile records and press Enter.

 

The subfile records will be processed in the order in which they exist in the subfile. Because this is a self-extending subfile, the user could page down and select other records for processing without losing what was entered on the previous page or pages. The SFLRCDNBR keyword in the DDS allows me to display the page that contains the last record selected for processing by the user. Once the Enter key is pressed, all the changed records will be processed. If the user were to select options and then position to somewhere else in the subfile using the position-to field, however, everything previously selected would be lost. Selecting Option 5 allows the user to view information from the master record.

 

Figure 4 shows the screen displayed when Option 5 is selected from the subfile list. Figure 5 displays the associated code.

 

070914Vandeverfig04 

Figure 4: The screen that displays a record's detail

 

When Option = Display; // Option 5 is entered in the subfile opt

 

Eval Mode = *Blanks;

 

Exfmt Panel2;         // Display the Display Detail screen.

 

Option = *Blank;     // Blank out option field.

 

Update Sfl1;         // Update the subfile.

 

If (Cfkey = Exit) or (Cfkey = Cancel);

 

   Leave;

 

Endif;

Figure 5: The code from the Process_Subfile subroutine that processes a detail request from the user

 

Figure 6 shows the screen that will be displayed when the user selects Option 2 (update). At first glance, it looks identical to the one in Figure 4, but there is a difference: the upper-left corner shows the word "Update." Another, more significant change is that the user is allowed to change the data fields on the displayed screen. This gives the user the ability to select subfile records and change the detailed information in them.

 

070914Vandeverfig06 

Figure 6: The screen used to update a record

 

Figure 7 shows the code for the screen in Figure 6.

 

When Option = Change;   // Option 2 is entered in the subfile opt

 

Eval Mode = 'Update';

 

Exsr Change_Detail;   // Display the Change Detail screen.

 

Option = *Blank;     // Blank out option field.

 

Update Sfl1;         // Update the subfile.

 

If (Cfkey = Exit) or (Cfkey = Cancel);

 

   Leave;

 

Endif;

Figure 7: The code from Process_Subfile that processes an update request from the user

 

Now let's talk about Option 4, which lets the user delete records from the data file. It's important, as well as courteous, to provide a confirmation screen before allowing the user to delete records from a data file. I do this by building a load-all subfile that contains the records the user has selected for deletion. When the READC loop is finished processing (that is, after all the records selected by the user have been interrogated), I check to see whether I loaded any records into the delete confirmation screen, by checking the relative record number (RRN2). If RRN2 is greater than zero, I display the subfile using the EXFMT operation and wait for the user's response. Figure 8 shows the delete confirmation subfile, and Figure 9 shows the associated code from the Process_Subfile subroutine.

 

070914Vandeverfig08 

Figure 8: Screen confirming a delete request

 

   When Option = Delete;   // Option 4 is entered in the subfile opt

 

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

 

     Write Sfl2;           // Write the deleted record to subfile.

 

   *In74 = *On;         // Mark record as changed.

 

     Update Sfl1;         // Update original subfile as changed.

 

     *In74 = *Off;         // Reset the SFLNXTCHG indicator.

 

 

 

Endsl;

 

 

 

Readc Sfl1;

 

Enddo;

 

 

 

// If records were selected for delete (4), throw the subfile to

 

// screen. If enter is pressed execute the Delete subroutine to

 

// physically delete the records, clear, and rebuild the subfile

 

// from the last deleted record (you can certainly position the

 

// database file where ever you want).

 

 

 

If Rrn2 > 0;

 

Lstrrn2 = Rrn2;

 

Rrn2 = 1;

 

Write Fkey2;

 

Exfmt Sf2ctl;

 

If (Cfkey <> Exit) And (Cfkey <> Cancel);

 

   Exsr Delete_Record;

 

   Setll (Dblnam) Sfl001lf;

 

   Exsr Clear_Subfile_1;

 

   Exsr Build_Subfile;

 

Endif;

 

Endif;

Figure 9: The code from Process_Subfile that processes a delete request from the user

 

Warning: Remember that the position-to in this self-extending subfile clears and rebuilds the subfile. If you use this example, be careful not to reposition the subfile with the position-to field unless you have processed all of your selections. Of course, you can change the logic to process the selected options before repositioning the subfile, by executing the Process_Subfile in the position-to when clause of the main routine, but I chose not to do that in this case.

 

Notice that when I write to the delete confirmation subfile (SFL2) in the Process_Subfile subroutine, I set on indicator 74 to activate the SFLNXTCHG keyword and update SFL1 with the UPDATE keyword. I then deactivate SFLNXTCHG by setting off indicator 74. This marks that subfile record for change, even though it has already been read by the READC operation and the user has made no other changes to that record. This is how I mark a record for change inside my program. Because I didn't clear the options field before updating the subfile, the "4" remains on the subfile record. This way, the user can choose to remove any records he or she doesn't want to delete, press Enter, and the records left marked "magically" show up on the delete confirmation screen.

 

Figure 10 shows the screen displayed if the user selects F12 from the delete confirmation screen. Notice that the records selected for deletion are still marked with 4s. I did this in case the user didn't like what he or she saw on the confirmation screen and wanted to "undelete" an individual entry or two. The user could simply press F12, remove the 4s from the records to "undelete," and then press Enter to get a new confirmation screen, without reentering all the records to be deleted. The SFLNXTCHG keyword makes this possible.

 

070914Vandeverfig10

Figure 10: Redisplaying the original choices when the user cancels from the delete confirmation screen

 

Figure 11 shows the Delete_Record subroutine, which is executed from the Process_Subfile subroutine when the user specifies delete. The program clears the original subfile (SFL1) and reloads it—minus the deleted records, of course.

 

Begsr Delete_Record;

 

 

 

For i = 1 to Lstrrn2;   // Loop until no more records to delete.

 

Delete (Dbidnm) Pfr;

 

Endfor;

 

 

 

Endsr;

Figure 11: The Delete_Record subroutine deletes the records selected by the user

Add to the Fun with Add Capability

This program also includes the ability to add records to the data file. I won't detail this logic because it really has nothing to do with subfiles. The user can press F6 to add a record to the data file. The program then displays a screen similar to that used for modification (Option 2 from the subfile). The user can enter the new information and add that data to the data file by pressing Enter. Once the add routine is finished, the program clears and reloads the subfile so it will include the newly added record.

You're Now Ready for Master File Maintenance

You've just seen a very useful template for a Name Master File Maintenance program. The user can view, change, and delete data from a data file. I use this type of subfile program when I'm working with files that will have a limited number of additions and modifications.

 

"What's limited?" you might ask. For me, limited is something of master file-like quality. Master files are not transactional files, meaning you won't have users pounding away at the keyboard adding data to them. My program doesn't lend itself very well to that kind of work. For one, you have to press F6 every time you want to add a record. This becomes very time-consuming if there are hundreds or thousands of records to add to a file, as is sometimes the case with transactional-type files such as an order-entry detail file.

 

If you'd like to learn a subfile technique that's not only able to keep up with the most skilled data-entry professional, but might even enhance his or her skills, check out more of my book Subfiles in Free-Format RPG.

 

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

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • 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.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • 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

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • 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: