Wednesday, November 20, 2013

Position record to top of subfile

position subfile sflrcdnbr

I recently received a message asking about subfiles:

I know this is a stupid question and I have done this a million times before, but I'm just blocked now and can't figure out what is wrong. My subfile is fully loaded, it is a few pages long. Enter was pressed on a second page, so I need the same page displayed when control is passed back to the subfile. For some reason it goes back to the first page.

The questioner must be asking about a "expanding" or a "load all" type of subfile, as this would not be an issue in the "page a time" subfile.

Fortunately there is a quick, and easy, solution for this problem by the addition of one line of code to the display file's source and a just two lines to the RPGLE/RPG IV's source.

In the DDS source code for the subfile the following line needs to be added to the subfile control record:

A            Z1SFLRCD       3S 0H      SFLRCDNBR(CURSOR)

The Subfile Record Number keyword, SFLRCDNBR, is used to specify that the subfile record number moved to the field is displayed on the current subfile page. Or, to put it another way, if you select a record on the second page of the subfile the second page is displayed.

If you specify *TOP, see below, then the selected record appears at the top of the current subfile.

A            Z1SFLRCD       3S 0H      SFLRCDNBR(CURSOR *TOP)

In your RPGLE/RPGIV program you have to move a value into the SFLRCDNBR field, if you do not you will get a "RNQ1255 - Session or device error occured in file..." error

Below is the code I used to load the subfile:

01 for Z1RRN = 1 to 999 ;
02    read TESTPFR ;
03    if (%eof) ;
04       leave ;
05    endif ;
06   write SFL01 ;
07 endfor ;

08 if (Z1RRN > 1) ;
09    IndDs.SflDsp = *on ;
10    Z1SFLRCD = 1 ;
11  endif ;

After loading the subfile from the file, TESTPF lines 1 - 7, I check, line 8, if the subfile contains any records. If it does then I turn on the SFLDSP indicator, line 9. I know I am not doing something like *IN01 = *ON, I use the Indicator Area in the display file and Indicator Data Structure in my program. To learn more see No more number indicators. Line 10 is where I have added the code to position the subfile to the first record, therefore, when the subfile is displayed it starts with its first record.

In the area of the code I perform the check to see if any of the subfile's records have been changed I add a line, see line 6, to move the subfile relative record number of the record to the SFLRCDNBR field. This will ensure that the subfile will display the record that was last changed.

01 dow (1 = 1) ;
02   readc SFL01 ;
03   if (%eof) ;
04      leave ;
05   endif ;

06   Z1SFLRCD = Z1RRN ;

07   Z1OPT = ' ' ;
08   update SFL01 ;
09 enddo ;

So what happens when the 19th record, which is on the second page of the subfile, has been selected?

With SFLRCNBR(CURSOR) the second page of the subfile is displayed as the 19th record is in it:

QUSER                  RPGPGM.COM - From AS400 to IBM i      TT:TT:TT DD/DD/DD
TESTSFL-1                  Example Subfile program                    MY_IBM_I


With SFLRCNBR(CURSOR *TOP) the subfile page is reposition so the 19th record is at the top of it:

QUSER                  RPGPGM.COM - From AS400 to IBM i      TT:TT:TT DD/DD/DD
TESTSFL-1                  Example Subfile program                    MY_IBM_I


Personally I use the SFLRCNBR(CURSOR *TOP) in my subfiles.

You can learn more about SFLRCDNBR on the IBM website here.


This article was written for IBM i 7.1, and it should work with earlier releases too.


  1. It is nice to see developers willing to help the community. .....I was recently asked to write a subfile program during a job interview. I stopped to think---it has been about 5 jobs ago that I was asked to write a subfile program---so I had to wing it. As most subfiles were written years ago as legacy. Yes- you still have to be able to maintain them.

    1. Tim,
      I can assure that subfiles still are core feature in any business RPG/COBOL applications involved users interface/screens regardless when they were built - recently I was asked on the interview at the big retail chain company "how I would handle empty subfile to pevent it from crash"... That's the reality.

    2. Maybe I was misunderstood. .....I was jus saying that most of the time we only do maintenance on the legacy subfiles. This is what my experience has found.

  2. Based on the discussion here it's clear that nobody develops display files for new applications, or they won't admit it :) What is everyone using for the user interface for new apps.... or are there any new apps being written?

    1. The companies I worked in lately.....for new apps, they were using different web tools, and dashboard dropdown menu GUI tools. Forgive me--- but I did not create the web screens. The screen were all custom made with languages like Java/JSP. I simply feed the data into the database so that the data could be extracted to the web screens from the AS400. They used the AS400 for a safe place to tabulate the data. The Web was used for a pretty user interface. The were getting the best of both worlds.

    2. Over the last year, I have developed new applications using Green screen (much to the chagrine of many on here), even subfile applications.

      But it's all in Freeform :)

  3. I've written a number of subfile programs and just wrote one using free-form RPG, but I always start with a template so I would have a hard time answering specifics about various keywords without a manual handy. Guess I would fail a job interview even though I could pop-out subfile programs pretty easy.

  4. Usually what I does is a have a made template of every type of subfile a long time ago and just put in place what I need and do the change accordingly.

    1. Same story with Jose P here. My company has developed (roughly 10 -15 years back) skeleton for any combination of subfiles possible in our business environment and it just works like magic. I used to handle subfiles using similar logic mentioned in the post but now wondering how much time programmers could have wasted on this rather than building a common skeleton like we have.

  5. I am working on subfiles at the moment so this post could not have come at a better time.


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.