Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
The AS/400 (Application System/400) was first introduced by IBM on June 20th,
1988. The AS/400 is a popular family of mid-sized computer systems which can also
be used as multiuser computer systems. By this, we mean that a single computer
can interact with more than one user at a time.
The AS/400 can be utilized for different business facets. Some models are designed
as systems that provide resources to other computers, also known as a "server" in a
network of computers, while others are set up for use with terminals or "display
stations". OS400 is the operating system for the AS/400. The AS/400 computers
offer more compatibility across the product line than the earlier System/3X
computers. Hence, the earlier IBM models of the System/36 and System/38 have
since been replaced by the AS/400 systems.
IBM has sold over 600,000 AS/400's and over 350,000 of them are still active. From
distribution warehouses to hospital administrators, and even manufacturing
companies, the AS/400 is a strong component in aiding these companies daily
business operations. The AS/400 utilizes a green screen interface, a built in
database that resembles DB2, and a vast array of software to provide business
solutions for todays business needs.
In October of 2000, IBM rebranded the AS/400 and announced its name as the IBM
iSeries 400. In doing this, IBM has shown that it is evolving this mid range family of
computers in an attempt to keep in tune with the fast paced demands and changes
that the Internet and the e-business world reside in. For the purposes of our tutorial,
we will continue to use the term AS400 rather than iSeries, although there is no
distinction between the two. As well, since 95% of AS/400's use RPG, we will
emphasize using RPG in this tutorial.
To answer the question of "why should you bother to learn to use the AS400", check
out some of the job sites on the web, such as computerjobs.com or monster.com,
and do a search on AS400 RPG, and see the career possibilities that await you.
You can run commands directly from any command line. Once you learn the commands, you
can write "Control Language" or CL programs. CL programs are simply strings of commands
that have been entered and compiled. So as you learn these commands, you are also learning
to write CL programs. Managers and administrators who have duties in different environments
may have trouble shifting gears from one operating system to another. We're going to look at
some easy ways to help you learn and remember these commands.
The AS/400 operating system is consistent in its presentation and names. Commands have
names of up to 10 letters. The commands typically take the form of three letters. For example,
to work with active jobs, the command is WRKACTJOB. That's a single word with no spaces.
WRK is the AS/400 abbreviation for "work" and ACT is the abbreviation for "active." Because
the AS/400 is consistent in its naming style, after you know some of the abbreviations, you will
be able to guess the names of commands.
1
In the sidebar are 11 important verbs and nouns that combine to form command names. The
abbreviations are not all three letters but they are used consistently throughout the system. "F"
is always the abbreviation for file. You won't have to guess if it is sometimes abbreviated as
"FIL."
If you don't know the exact name of a command, you can use the AS/400 menus to browse. If
you key in GO VERB from a command line and hit Enter, you will go to the AS/400 menu
named VERB. This menu lists all AS/400 verbs. From here you can navigate and browse to the
exact command you need. Also, to see the major command groups, GO MAJOR.
If you already know the verb, but need the rest of the syntax, there is a menu named after the
verb. For example, there is a CMDWRK menu with all "work" commands, there is a CMDDSP
menu with all "display" commands and there is a CMDADD menu with all "add" commands. So
you can key in GO CMDSTR and see all "start" commands.
To learn more about any command, type in the number of the command and then hit F1. The
AS/400 has extensive, friendly help built into every command.
Some innocuous looking commands such as CPYF are deceptively powerful and complex.
CPYF can copy a file, renaming it while selecting only records in which the field "STATE" has a
value of "DC." In fact, there are six screens of key words you can use. Learning this one
command can give the power to clean up a master file by copying only records you wish to keep
while bypassing data created by an errant task.
-sidebar-
It is important to become familiar with basic AS/400 command abbreviations. The following is a
list of the most commonly used verbs and nouns that combine to make commands.
Command Abbreviations--Verbs
ADD - Add
CFG - Configure
CHG - Change
CPY - Copy
CRT - Create
DLT - Delete
DSP - Display
GO - Go to a Menu
RMV - Remove
STR - Start
2
WRK - Work with
Command Abbreviations--Nouns
DEV - Device
F - Files
FLR - Folder
LIB - Library
MSG - Message
Library Lists
The library list on the AS/400 is similar to the "path" on PC's. Mainframe programmers may
compare it to the "steplib" or "joblib". It defines the order of libraries that the operating system
uses to find objects.
When you execute a command or call a program, the AS/400 must know where to find the
command or program. Sometimes, you specify the library explicitly. But if you don't, the AS/400
will find your command or program by looking in each library in your library list. It will use the
first one that it finds first.
This is very handy for testing. If you are testing a version of a program, you can put it in a test
library which is at the top of your library list. Then, whenever you specify a program or refer to a
file, the system will use any object it can find in the test library. If it can't find it there, it will look
further through your library list until it finds it.
2) Put objects that you want to test in the library. These can be files, menus, programs or any
AS/400 object.
3) Insert the test library name into your library list, before your production libraries.
Now you can execute test programs and modify test data. The best part is that you don't need
to copy production programs or files into the test library unless they will be affected by the test.
That is, all those supporting database files that you need to run a program, but that are used for
3
input only, do not need to be copied to the test library. Of course, if you are unsure if a file will
be modified during your test procedure, copy it to the test library anyway.
Once you have inserted the test library name in your library list, you will want to make sure
everything is in the proper order. Use the DSPLIBL command to see your library list. You will
probably see three types of list entries:
1) The SYS libraries have operating system objects. This is where the AS/400 commands and
menus are contained.
2) The CUR library is your current library. This is your first user library and is where objects are
created if you don't specify which library to use.
Use EDTLIBL to change your USR libraries. You can rearrange the list, remove libraries or add
libraries. Notice that you can't see the SYS or CUR libraries when you use EDTLIBL. The only
way to change the current library is with the CHGCURLIB command.
The WRKOBJ command will help see which object is being used. This command is similar to
the directory ("dir") command on PC's. Use it to list objects in your library list.
So, if you have three copies of program, "PAYCALC", in different libraries, use the command
WRKOBJ PAYCALC to see which copy of "PAYCALC" will be used. The command will list all
objects named "PAYCALC" in the sequence that the AS/400 finds them . If you ever say to
yourself in frustration, "I know I changed the program but it looks like I'm still running the original
program", it's a pretty good bet that you're executing a different copy of the program than you
think. This is only a fraction of the power of the WRKOBJ command. It is the easiest way to
navigate through an undocumented system.
When you are copying files to your test library, use special care with the logical files. It is easy to
create a logical file that resides in a test library but updates a physical file in a different library.
QHLPSYS SYS
4
QUSRSYS SYS
QTEMP USR
Before you work on this topic, there is an important option to change. From the command line, key in the
command STRPDM and hit ENTER. Then, key in 1 and hit ENTER. Then hit ENTER again. Your screen
should look like:
-------------------------------------------------------------------
QHLPSYS *PROD-SYS
QUSRSYS *PROD-SYS
QPDA *PROD-PRD
5
QADM *PROD-PRD
QTEMP *TEST-USR
QGDDM *PROD-USR
QGPL *PROD-USR
--------------------------------------------------------------------
Now, hold down the SHIFT key and hit F6. You should now see:
---------------------------------------------------------------------
Change Defaults
4=Direct Chain
6
--------------------------------------------------------------------
Hit TAB until the cursor is on the "Compile in batch" line. Change the value from Y to N and hit ENTER.
Programs are created from source statements. On the AS/400, source statements are entered using PDM,
the Program Development manager. Technically, PDM uses the Source Entry Utility (SEU) as the editor.
But you don't have to think of SEU as something different from PDM.
PDM provides a way to enter program source statements and then to compile the statements into
executable programs. The source statements are needed only for the compiler. The compiled programs are
the objects that are actually run on the AS/400. The source code and the compiled objects are separate
things. But it is easy to confuse the two. At times, you may try to open the compiled object with PDM
when you really mean to open the source code used to create the compiled object.
Source code is kept in a member of a file. The file is kept in a library. Each file can have many members.
Each member is the source code for a program.
The source code files can have any name but conventionally the names start with Q and end with SRC,
for source. Typically:
You could keep most of the source statements is a single file. That is, one member could have CL source
statements and another member could have DDS and another could have RPG source statements.
Conventionally though, source code is not mixed up like that.
So, let's try it out by writing a simple CL program to change the "Current Library" to your library and to
go to the programmer menu. CL is the Control Language of the AS/400. This is a lesson in PDM so don't
fret too much about the specifics of CL.
When you got your user ID on this system, a library was also created for you with the same name as your
user ID. Suppose that your ID is USER999. That means you have a library available to you named
USER999. Some people relate an AS/400 library to a Directory or Folder on a PC.
Your library is probably empty. Since this is the first CL program ever written in your library, you need to
create a file to store the CL source statements in.
CRTSRCPF and then hit F4. (F4 always invokes the prompts)
From the earlier lesson in commands, you should remember that commands usually are made up of 3
letter abbreviations of verbs and such. In the case, the command CRTSRCPF stands for Create Source
Physical File. That's not exactly intuitive but it's not too hard to remember.
For the library name, key in your library name, similar to USER999. For the Object (or File) name, key in
QCLSRC. Now hit ENTER and you've created a source physical file named QCLSRC in the library
7
named USER999. Remember that QCLSRC is not a magic or reserved name. But almost everyone uses
this name for their CL source.
Once you use commands for a while, you will learn how to enter simple commands without the prompts.
In this case:
CRTSRCPF USER999/QCLSRC
So now you have a place to store your CL source statements. Lets start PDM and try to enter the simple
program. PDM can be started from the programmer menu or with the Start PDM command. Most
programmers use the Start PDM command. Type in STRPDM and hit ENTER.
------------------------------------------------------------------------
Selection or command
-----------------------------------------------------------------------
8
Now for the most confusing part of PDM… And it's not that confusing. PDM will let you navigate to
your source statements 3 different ways. Let's try to draw a picture of the organization of the source
statements.
Suppose you have 2 libraries. One for General Ledger programs and one for Payroll. Each one has CL,
DDS and RPG programs. A schematic of this is:
GLC003
GLC007
QRPGSRC GLR001
GLR002
GLR005
QDDSSRC GLD001
PRC101
PRC125AB
QRPGSRC PRR100
PRR125
QDDSSRC PRD105
PRD107
PRD108
The analogy on a PC is that you have two Folders in the root; one is GLLIB and the other is PRLIB. Each
of those folders has three folders: QCLSRC, QRPGSRC and QDDSSRC. Each of those folders has a few
documents, each one with the source statements for the program.
USER99 QCLSRC
9
That is, you have one file in your library and that file has no members.
I prefer to navigate to my source code using selection 3 (Work with members). So, key in 3 and hit
ENTER.
------------------------------------------------------------------------
Member:
------------------------------------------------------------------------
Key in the File name of QCLSRC and the library name like USER999. Finally for member name key in
*ALL.
This is a little like saying on a PC, "Show me all the documents in the folder QCLSRC which is in the
folder USER999".
The AS/400 now shows you the "WORK WITH MEMBERS WITH PDM" display and all of the
members in QCLSRC. But since QCLSRC has no members it shows "NO MEMBERS IN FILE".
About ¼ from the top, the AS/400 tells you what options you can use in PDM. For example, 2 is to edit or
change a member, 3 is to copy a member, 4 to delete a member. If you hit F23, the AS/400 will display
more options. Your keyboard has only 12 "F" keys at the top. If you hold down the shift key when you hit
an "F" key, it adds 12 to the "F" number. So, if you hold down shift and hit F11, this is the F23 key.
When you do this, the AS/400 shows other option numbers.
Similarly, at the bottom, the AS/400 tells you what "F" keys you can use. Hitting F24 (shift while hitting
F12) will cause the AS/400 to display more "F" keys. These "F" keys are called FUNCTION KEYS.
So, finally.. Let's add a member and write a simple Control Language program.
At the bottom-right of your screen you should see that F6=Create. This means that hitting F6 will let you
create a new member. So, hit F6.
10
The screen will now show:
------------------------------------------------------------------------
------------------------------------------------------------------------
Fill in the Source member name. This will become the program name. Name it FIRSTCL.
The Source type is important. It will tell the AS/400 what compiler to use to compile the source code. CL
programs are designated as Source type CLP. So, enter CLP.
-----------------------------------------------------------------------
SEU==> FIRSTCL
'''''''
'''''''
'''''''
'''''''
'''''''
11
'''''''
'''''''
'''''''
'''''''
'''''''
'''''''
'''''''
-----------------------------------------------------------------------
You now have an empty member to key source code into. If you were to hit ENTER right now, the editor
(SEU) would assume that you don't want to keep any of these lines since you haven't entered anything on
them.
So, to make things simple, key a space on the top line, then TAB twice to the next line and key another
space. Do this on about 10 lines.
Now, when you hit ENTER, SEU sees that you keyed something (a space) on those lines and it keeps
them for you.
If you did this, SEU replaced the dots on the left with numbers and should look like:
------------------------------------------------------------------------
SEU==> FIRSTCL
0001.00
0002.00
0003.00
0004.00
0005.00
0006.00
0007.00
0008.00
0009.00
12
-----------------------------------------------------------------------
Languages have syntax rules to follow. CL is mostly a free format syntax. Blank lines are allowed and
commands can be placed in any column. Also, CL programs must begin with a PGM statement.
So, key PGM on the first line and hit ENTER. Your screen should now look like:
------------------------------------------------------------------------
SEU==> FIRSTCL
0001.00 PGM
0002.00
0003.00
0004.00
0005.00
0006.00
0007.00
0008.00
0009.00
-----------------------------------------------------------------------
Now, it's time to experiment a bit with the line editor. SEU (the name of the editor being used by PDM) is
a line editor. That just means that commands to copy, insert and delete lines are entered in the left portion
of the screen where the numbers are.
Try a simple line command. Key in D right on top of the number 0001.00 at the top left. your screen
should look like:
------------------------------------------------------------------------
SEU==> FIRSTCL
13
D001.00 PGM
0002.00
0003.00
0004.00
0005.00
0006.00
0007.00
0008.00
0009.00
-----------------------------------------------------------------------
This D tells the editor to delete this line when the ENTER key is hit. So, go ahead and hit ENTER and
watch that line disappear.
Just to experiment, copy line 0001 and insert it after line 0005. Do this by keying a C (for "copy") on line
0001 and an A (for "after") on line 0005.
------------------------------------------------------------------------
SEU==> FIRSTCL
C001.00 PGM
0002.00
0003.00
0004.00
A005.00
0006.00
0007.00
0008.00
14
0009.00
-----------------------------------------------------------------------
When you hit ENTER, SEU will copy line 1 and place it on a new line after line 5.
If you key C3 on line 0001, SEU will copy three lines and place them after the line with an A.
Or, if you key CC on line 0001 and CC on line 0004, SEU will copy from line 0001 thru 0004.
Similarly, putting DD on line 0001 and DD on line 2000 would delete all lines from line 1 to line 2000.
Okay, let's finish the program. The complete program will do only two things. The first is that it will
change your "CURRENT LIBRARY" to your library USER999 and the second thing is it will go to the
Program menu on the AS/400.
Since this isn't a lesson in CL programming, trust me and key in the following program. Be sure to use
your USERID in place of USER999.
------------------------------------------------------------------------
SEU==> FIRSTCL
0001.00 PGM
0002.00
0004.00
0005.00 GO PROGRAM
0006.00
0007.00
0008.00
0009.00
-----------------------------------------------------------------------
Now hit F3 to tell PDM that you are done. Hit ENTER to accept the values on the screen. PDM has saved
your program and you can now see it in your PDM list of members.
15
---------------------------------------------------------------------
File . . . . . . QCLSRC
---------------------------------------------------------------------
So, you have now entered the statements needed by the AS/400 compiler to compile a CL program. Each
time you add a member, this list of members will grow. Just for fun, key a 2 next to FIRSTCL under OPT
on the screen. Hit ENTER and PDM will open the source file up again so you can see and change it. Hit
F3 and ENTER to get back.
To compile the program, key 14 under OPT next to FIRSTCL and hit enter. This tells PDM to compile
the statements that are in the member named FIRSTCL.
If all went well, in a few seconds, the AS/400 will have compiled your program. Hit TAB to get to the
bottom of the screen. This is the command line.
Key CALL USER999/FIRSTCL on the command line (be sure to use your USERID in place of
USER999). If your program compiled, you should now see:
-----------------------------------------------------------------------
PROGRAM Programming
1. Programmer menu
16
2. Programming Development Manager (PDM)
3. Utilities
Selection or command
-----------------------------------------------------------------------
That's the end of this lesson. Try not to be frustrated with the lack of user friendliness of PDM. The
wonderful news is that 99% of AS/400's use PDM. So anytime you log on to an AS/400, you will be
familiar with the source code editor.
It takes most people a week or so of using PDM to get comfortable with it. But you must learn PDM well.
You will use it to create files, logical files (indexes), display screens, print files, RPG programs, CL
programs and more.
Take a break and review the following link to IBM's publication site.
Finally, e-mail me and tell me how your first hands-on experience went.
Thanks,
David
dmount@texas400.com
Additional Material
http://publib.boulder.ibm.com/pubs/html/as400/online/v4r2eng.htm
17
Click on "SEARCH OR VIEW ALL BOOKS"
Key in SEU and click on FIND.
It should find the book titled "Source Entry Utility". Chapter 13 is a tutorial on entering source code.
If you have read through the topics you are probably getting a bit bored. It's time to jump in and start
trying things out.
First, it is important to learn a bit more about your library list. I hope you read the earlier topic on
LIBRARY LISTS and understood some of it. Sign on to the AS/400, and from a command line, key in
DSPLIBL and hit ENTER.
QHLPSYS SYS
QUSRSYS SYS
QTEMP USR
QGDDM USR
QGPL USR
This simply means that whenever you key in a command, call a program or go to a menu, the AS/400 will
look for what it needs in those libraries and in that sequence.
In fact, when you keyed in the command DSPLIBL, the AS/400 looked for it in the first library, QSYS.
Not by accident, that is the library that has all of the AS/400 commands. So, the AS/400 executed the
command DSPLIBL that it found in the library QSYS.
Since we will do a lot of things in your personal library, it will make things easier if your library is in the
library list. For now, the best way to do this is to make your personal library the CURRENT LIBRARY.
The CURRENT LIBRARY is a slightly special designation. The CURRENT LIBRARY is the first user
library. Also, if you create files and don't tell the AS/400 where to put them, it will put them in the
CURRENT LIBRARY.
Key in the command to change the CURRENT LIBRARY to your personal library. The command is
Change Current Library. This is an excellent example of the way the AS/400 usually makes the command
name from the 3 letter abbreviations. The command is CHGCURLIB. So, key in:
CHGCURLIB USER999
Of course, replace USER999 with your user ID. When I set up your user ID, I created a library with the
same name as your user ID. So, don't get confused and think that this command changes the current
library to your user ID. It is changing your current library to a library that happens to be named the same
as your ID.
18
Now display your library list again. You remember, DSPLIBL.
QHLPSYS SYS
QUSRSYS SYS
USER999 CUR
QTEMP USR
QGDDM USR
QGPL USR
Now let's have some real fun. There is a file in library USER000 named CUST. It has a little over 1,000
records in it. Some people think of a record like a line on a spreadsheet.
We will need some data for a query we will write in a minute. So, let's copy this file to your personal
library. In doing this, we will learn about AS/400 commands, the very powerful COPY FILE command
and a few other commands as well.
The command to copy the CUST file from the library USER000 to your personal library, USER999 is
pretty scary looking. It is:
Memorizing the keywords and options would be impossible. Fortunately, it's not that hard. Let's see how
the AS/400 makes it easy to use complex commands.
First, you must remember the command name. This command, CPYF (COPY FILE) is a common
command. But if you need help remembering it you could try going to one of the menus that lists the
commands.
If you remember that COPY is abbreviated CPY, you could key in:
GO CMDCPY
GO MAJOR
You would see a list of the major groups of commands. Since you see "more" in the lower right hand of
the screen, you know that you can hit the PAGE-DOWN key to see more. You don't need to though since
you can see that selection 5 will show you the FILE COMMANDS. you will find CPYF on that list. Or
19
you could have selected 2 for VERB COMMANDS and found the COPY COMMANDS as #18 on that
list.
Are you started to get the idea? This menu (most of them start with CMD) show you all of the commands
on the system.
Remember, we were looking for the command to copy a file. It is CPYF. So, let's see how to use a
command now that we know its name.
Key in the command on the command line but don't hit ENTER. Instead, hit F4. The AS/400 understands
that F4 means that you want to see the options and the prompts for the options.
Bottom
20
F3=Exit F4=Prompt F5=Refresh F10=Additional parameters F12=Cancel
With just a little experimentation, you will learn that the critical values on this screen are:
Of course, replace USER999 with yuour personal library name. Hit ENTER now and see how quickly
these 1000 records are copied into a file in your personal library.
You now have a file named CUST in your personal library and it has over 1000 records of data in it.
Now, you can learn some important things about the way commands work. You can hit F9 to RETRIEVE
the last command executed. This is a handy shortcut. But it also shows exactly what command was
executed and what the options were. So hit F9 and you will see:
For simple commands, the keywords (FROMFILE, TOFILE and CRTFILE) are not needed. That is, if
you key in all the right options in the right order, you may leave out the keywords. For a command as
complicated as CPYF, that is almost impossible. Most commands are simpler though.
To see the data in the file, you might expect to use a command DSPF for display file. As it turns out the
command is actually, DISPLAY PHYSICAL FILE MEMBER and is named DSPPFM.
Now that you know the name of the command, you know that you can key DSPPFM and hit F4 to see the
keywords and options. If you do, you'll see:
21
Display Physical File Member (DSPPFM)
File . . . . . . . . . . . . . . Name
That's not too bad. The only options are the name of the file, the library the file is in, the member (we'll
discuss this in another topic) and the first record number.
Notice that most of the default values are filled in with the value that you probably want. For example, the
FROM RECORD# is already set to 1.
So all you really need to fill in is the FILE NAME of CUST and the LIBRARY name where the FILE is
located. In fact, it's even simpler than that. The default value for the LIBRARY is *LIBL. This means that
the command will look for the FILE in the LIBRARY LIST. And since your personal library is the
CURRENT LIBRARY, all you need to enter is the file name.
So if you fill in the FILE name as CUST and hit ENTER, you should see a screen full of customer name
and address data:
22
001234REID 123 MAIN STREET ANYTOWN
Now to keep learning about how commands are executed, hit F9 to see the command that you just ran. It
is:
DSPPFM FILE(CUST)
What happened is that the AS/400 followed a line of logic like this:
I'm supposed to run the command DSPPFM so first I need to find the command in one of the libraries in
the library list. The first library in the library list is QSYS and it has a command named DSPPFM so I'll
run that.
Next, I need to find a file named CUST. There is no CUST file in the first library QSYS. There isn't a
CUST file in QHLPSYS or QUSRSYS either. There isn't a CUST file in QPDA or QADM. There is a
CUST file in USER999, which happens to be the CURRENT LIBRARY. So, I'll run the command
DSPPFM that I found in the library QSYS and I'll display the file CUST that I found in USER999.
Like many commands, this one is so simply that you don't need to prompt the keywords. You could just
key in:
DSPPFM CUST
By the way, you could display the original CUST file that you copied from USER000. Key in DSPPFM ,
hit F4 and fill in the file name of CUST and the library name of USER000.
DSPPFM USER000/CUST
Just for fun, delete the file you copied and then copy it again.
To delete the file, use the command DELETE FILE, which is DLTF. So if you key in:
DLTF CUST
or
DLTF USER999/CUST
and hit ENTER and you will delete your CUST file.
Now, use the CPYF command to copy it again from the USER000 library. Or, if you are efficient, hit F9
a few times until your CPYF command is retrieved and then hit ENTER and run it again.
The data displayed with the DSPPFM command is unformatted. That is, it is just a string of letters and
numbers. Try the command:
RUNQRY QRYFILE(CUST)
This command is a tiny part of the QUERY/400 programming language. It displays the file but separates
the data into fields with headings. It should look like:
23
Position to line . . . . . Shift to column . . . . . .
Line ....+....1....+....2....+....3....+....4....+....5....+....6....+....7.
You can see from the prompts at the bottom of the screen that you can shift the data to see more by hitting
F20. F20 is SHIFT and F8. F19 will shift back.
I hope you're having fun and starting to see the power of the AS/400.
Notice when you shift to the right by hitting SHIFT and F20 that the field name for the STATE is CSSTE.
For the last lesson in executing commands, let's copy the original file but select only customers from
Texas (where else?). We know that the field that has the state is CSSTE.
DLTF CUST
Now, key in the CPYF command and hit F4. As before fill in the file names and library names and the
create file option.
24
Notice that at the bottom, F10 will show ADDITIONAL PARAMETERS. If you hit F10, you will see
"more…' at the bottom right of the screen. This means that you can hit the PAGE DOWN key to see more
options.
The CPYF command has many, many options. If you hit PAGE DOWN 4 times, you will see:
Field . . . . . . . . . . . . Name
Value . . . . . . . . . . . .
More...
Here, you can enter selection values. Fill in the top of the screen to look like:
25
Include records by field test:
Value . . . . . . . . . . . . 'TX'
These values say, "copy only records in which the field CSSTE is equal to "TX""
Now if you hit enter, you will copy CUST from library USER000 to USER999 but it will copy only
customers from Texas.
When you have run this command, re-run the RUN QUERY command to see the data:
RUNQRY QRYFILE(CUST)
That's it for now. Experiment with your new knowledge. Use CPYF to copy your file. Use DLTF to
delete it. Use DSPPFM and RUNQRY to look at the data.
Most AS/400's have the IBM provided query language QUERY/400. If is an easy to use and powerful
tool for viewing and printing data. Here, we will write a query to see some of the data in your CUST file.
Then, we'll copy it and change it to create a printed report. Then, we'll learn how to view printed reports.
Finally, I'll have a few comments on how you could actually print reports on your laser printer (don't get
too excited though, its expensive).
Make sure you have read through the earlier topic "Library Lists and Copy File". In that topic you should
have copied a file named CUST to your user library. From here on, whenever you see USER999 be sure
to substitute your user library.
There are several ways to get to the Query programming screen. My favorite is with the command
WRKQRY. After keying in WRKQRY and hitting ENTER you should see:
----------------------------------------------------------------
5=Display, 6=Print
26
8=Run in batch, 9=Run
-----------------------------------------------------------------
This interface is easy to use but is different from PDM. One of the unexpected things is that you can write
a query without ever naming it. Of course, you must name it if you wish to save it.
We are going to write a query to view the data in the CUST file. Key in 1 for the option to create a query.
We'll name it later so for now just hit ENTER.
-----------------------------------------------------------------
1=Select
Select records
27
Specify processing options
-----------------------------------------------------------------
If you have ever worked with a database query product (like Microsoft's Access) this will have a
familiarity to it. Microsoft Access needs about the same information as the AS/400. It just presents the
options in a way that you point and click. With QUERY/400, you must view lists and select by keying in
"1" next to each item.
The most important question is "what file are you displaying?" This is so important that the AS/400 has
already put a "1" next to "Specify file selections". SO, just hit ENTER and you will see:
--------------------------------------------------------------
file selection.
---------------------------------------------------------------
You are simply specifying which file you want to query. Enter CUST and the file and USER999 as the
Library and hit ENTER. Hit ENTER again and you go back to the previous screen.
Now, select fields you want to see. Do this by keying "1" next to "Select and Sequence Fields". Use either
the TAB key or the "down-arrow" to get to that line and key in "1" and hit ENTER.
28
You should now see:
----------------------------------------------------------------
Type sequence number (0-9999) for the names of up to 500 fields to appear in the
report,
press Enter.
CSCTY CITY
CSSTE STATE
----------------------------------------------------------------
Since the AS/400 knows the file you want to see, it has retrieved the fields in the file and listed them for
you. If there were more fields, you would see the now familiar "more..." at the bottom right. "more..."
tells you that if you use the PAGE-DOWN key you will see more.
29
So, key in a sequence number next to each field. Key 2 next to CSNBR, 1 next to CSNAME, 3 next to
CSADR1, 4 next to CSCTY, 5 next to CSSTE and 6 next to CSZIP. Then hit ENTER twice and you'll be
back at the main screen.
One of the great things with QUERY/400 is that you can work on query, then view it, then change it,
view it again and change it until it is what you want.
F5 says that F5=Report. Maybe it should say F5=Run. If you hit F5 the query will run. It should look like:
---------------------------------------------------------------
Display Report
....1....+....2....+....3....+....4....+....5....+....6....+....7
-----------------------------------------------------------------
Just like that you have written a query to display data from an AS/400 database file. Notice a few things
about the query display: it says "more..." so you can hit PAGE DOWN to go down a page. At the top is an
area to position to any line. You could key in 50 and hit ENTER and the query would advance to line 50.
You can also enter "B" (for bottom) and hit enter to go to the bottom or "T" to get back to the top.
Notice the F-KEY prompts at the bottom of the screen. If you hit F20 (Shift F8) you will scroll to the
right. F19 (Shift F7) scrolls back to the left. You can even split the screen with F21 (Shift F9).
Once you have played around with that for a while, hit F12 (cancel) to get back to the main screen.
30
Now change the titles of the rows to be description. Do this by keying "1" next to Column Formatting.
Key in more descriptive columns like Name, Customer#, etc.. You can use all three lines for the column
heading.
You might revisit the "Select and Sequence Fields" selection to add a field or move fields around.
If in the process of working on the query you hit F3 at the wrong time, you will see the EXIT screen
which looks like:
-------------------------------------------------------
2=Run in batch
Query . . . . . . . Name
Text . . . . . . . .
----------------------------------------------------------
You can hit F12 to get back to your query. Boy that wasn't very obvious was it?
Now key "1" next to "Select Sort Fields". Key a "1" next to CSNAME and hit ENTER. Now hit F5 and
your query will sort the data by name when it displays.
31
Notice that this is real-world data. Some of the names start with a space and are therefore sorted to the
beginning.
Finally, key a "1" next to select records. Lets select only the records for accounts that live in "TX". Fill in
the screen to look like:
----------------------------------------------------------
Select Records
Tests: EQ, NE, LE, GE, LT, GT, RANGE, LIST, LIKE, IS, ISN
CSSTE EQ 'TX'
----------------------------------------------------------
Now when you hit F5 you will see only accounts in Texas and in sequence by name.
You can also create totals of columns and break totals. You can specify if you want details (like this) or
summary only.
For now, quit and save this query. Hit F3 and fill in the screen to save the query as CSQ001.
-------------------------------------------------------------
2=Run in batch
32
For a saved definition:
---------------------------------------------------------------
RUNQRY USER999/CSQ001
Your query should display what you saw earlier when you hit F5.
So, that was pretty incredible. In just a few minutes, you wrote and saved it so that it can be run from the
command line. Next topic will show you how to put that on a menu.
To do that, work with queries again (WRKQRY). Then fill in the screen like:
-----------------------------------------------------------------
5=Display, 6=Print
--------------------------------------------------------------
Hit ENTER and you will see the "Copy Queries" screen. Under "To Query" change the query name from
CSQ001 to CSQ002 and hit ENTER.
There is now a query named CSQ002 that is identical to CSQ001. Open that query up so you can change
it to print. Do this by keying:
33
--------------------------------------------------------------
5=Display, 6=Print
8=Run in batch, 9=
---------------------------------------------------------------
Put "1" next to "Select Output Type and Output Form" and hit ENTER.
---------------------------------------------------------------
2=Printer
3=Database file
2=Summary only
34
Wrapping width . . . . . . . . . Blank, 1-378
---------------------------------------------------------------
You can see that the AS/400 defaults each query to display detail information without line wrapping. You
can experiment with the line wrapping later. For now, change the Output Type form 1 to 2 and hit
ENTER.
On the "Define Printer Output", hit ENTER to accept all of the defaults. Also hit ENTER for the "Define
Spool Output" screen.
You can try different values here but for now, change the "Print Cover Page" form "Y" to "N" and hit
ENTER.
You should now see the "Specify Page Headings and Footings". Fill it in with something like:
----------------------------------------------------------------
Print standard
Page heading
Customer File
Texas Only
Sorted by Name
Page footing
CSQ002
---------------------------------------------------------------
I've put in a three line heading and entered the query name as a footer. Hit ENTER to get back to the main
query screen.
35
Note that if you hit F5 right now, you will see exactly what the other query saw. The headings and footers
are not displayed. They will show up in the report though.
Now run the query from the command line by keying in:
RUNQRY USER999/CSQ002
The query has now created a report and written it to the Spool File. The Spool File is a storage area for
the report. If you had an AS/400 printer connected and ready to print, the file would print to the report.
Since you don't have an AS/400 printer, the report is still sitting in the Spool File.
There are many Spool File areas on the AS/400. Which one is yours in? It depends on the values used to
set up your user profile. That is, when a user ID is defined, the printer for that user ID is also defined. In
your case, it isn't really a printer... it's a dummy printer.
So, how can you look at the reports in your Spool File. You use the "Work with Spool Files" command.
Key:
WRKSPLF
You will see the Spool File with all the reports in it. Many of them will have odd names and some may be
error logs generated automatically. For example, if you disconnect from the internet before you sign off,
the AS/400 may generate a system log report of your "abnormally terminated" job.
Reports created by QUERY/400 are given the messy name of QPQUPRFIL. So you should now see a list
of all the reports in your Spool File.
Near the top of the screen you can see the options you can use. The only ones you should need are
4=DELETE and 5=DISPLAY.
By keying the option number "5" next to a report and hitting enter, you can see the report. Here you can
see your headings. Similar to the interface for QUERY/400 you can: F20 to scroll right (shift F8)
F19 to scroll left (shift F7)
Key in "B" and hit ENTER to go to the bottom
Key in "T" and hit ENTER to go to the top
Once you've looked at your report, exit out of it by hitting F3. Now put a "4" next to it in the Spool File
list and hit ENTER and you have deleted it.
Experiment by making several reports. Do this by running your query several times. Then use WRKSPLF
to view your spool file.
Feel free to delete all of the reports in your Spool File. I try to keep mine empty. Especially, feel free to
delete the reports you don't understand because they were created by the system.
Not too long ago, all AS/400 printers were connected directly to AS/400's or AS/400 remote controllers.
With the internet has come the ability to easily route print data to a remote. Doing so requires the client
machine (that is, your PC) to have an AS/400 Telnet Client that is capable of creating virtual print
devices.
36
With such a Telnet Client, you can easily print reports, like this query, directly to your printer. These
Telnet Clients are about $150 to $200. I like the one sold by Synapse (www.synapse.com). There are
others including IBM's Client Access.
You do not need print capabilities to do this tutorial. I rarely print reports out. I never print programs out.
It is important to realize how easily your reports could be printed on a PC printer though. A $200
investment for a company to be able to use a remote AS/400 is not very much money.
The AS/400 database, DB2/400 is part of the AS/400 operating system. It is a relational
database and has features similar to other databases you may have used such as Microsoft
Access, Oracle or Dbase. The standard method of creating files is to define the specifications of
the file using DDS. Of course DDS is used for other things on the AS/400 like defining display
files and print files.
To create a physical database file, enter your source statements in a DDS source file member.
For now, name the member "CUS" for customer file. Make sure the member type is "PF". This
tells the compiler that this is for a physical file. Enter the records in Figure 1 to define a simple
customer file. Notice that the first record has an "R" in position 17. This means that this line is
specifying the record name, in this case "CUREC". The "TEXT" is optional but helps to
document the file.
After the line naming the record, each line describes a field. The field name is followed by the
field length and its data type. The three most used data types are "A" for alpha or character, "S"
for numeric and "P" for packed decimal. For numeric fields, you must also specify the number of
decimal positions. So here, the company number field is a three digit number with no decimal
places. The customer number and zip code are also numeric fields with no decimal places. The
credit limit is a numeric, packed decimal field with 9 digits, of which two are after the decimal
point. The rest of the fields are character fields.
Once you have entered the DDS source code, you must compile it. You do this by keying option
14 next to your member name on the PDM screen. If you pay attention you will see that the
AS/400 is actually executing the CRTPF (Create Physical File) command. It knows to use this
command because the member type of the source code is "PF".
You now have a database physical file. This file has built into it the fields and their attributes.
Let's modify this file definition to add key fields. If a physical file has key fields, programs can
randomly access the records or read them sequentially in the order of the key fields. You can
see in Figure 2 that it is simple to add key fields.
The "UNIQUE" record at the beginning of the source is used if you want the AS/400 to insist on
having no records with duplicate keys. This is optional.
At the end of the source code, there are two lines with "K" in position 17. These lines define the
key fields. So, this file will build a key using the company number and then the customer
number. Further, it will not allow a duplicate company number / customer number records to be
written.
37
The records are written to the file in arrival sequence. If you read the data by specifying keyed
access, the records will read as though they have been sorted by company number and
customer number. Also, your programs can randomly retrieve records. For example, a "CHAIN"
instruction in RPG can now randomly read the record for a specific company number / customer
number.
Now for the real magic. In only a few lines of source code, you can create a logical file. This is a
way of reading the file with a different key. A logical file is a list of key values that point to the
physical file. It does NOT contain copies of the data in the physical file.
To create a logical file, again enter your source statements in a DDS source file member named
"CUS01". This time, make sure the member type is "LF". Enter the source code in Figure 3.
The first line tells the compiler that this logical file will resequence the records in the physical file
named in the "PFILE" specification. In this case it is the "CUST" file. Also, notice that here we
are using the same record name as the physical file, "CUREC".
The only remaining lines are like the last two lines of the physical file source. They name the
fields that you want to use as keys.
Again compile the file by using option 14. This time, the AS/400 will execute the "CRTLF"
(create logical file) command. You now can read the customer file in city / state sequence
simply by reading the file "CUS01" in keyed sequence.
Finally, look at Figure 4. The "S" type record will select only records that have customer status
equal to "A". Also, this logical file will read the records in customer name sequence.
So, a program reading this file in keyed sequence, will read the records alphabetically by
customer name and will read only records of active customers.
38
A CUSTAT 2A TEXT('CUSTOMER STATE')
A UNIQUE
A K CUCO
A K CUNUM
A R CUREC PFILE(CUS)
A TEXT('CUST BY CITY /
STATE')
39
A K CUCITY
A K CUSTAT
A R CUREC PFILE(CUS)
A TEXT('CUST NAME/ACTIVE
ONLY')
A K CUNAME
It is important to understand the history behind the RPG language. The language was originally
developed in the 1960's. It was a very high-level language meaning that a lot could be done with few
programming statements. It was originally intended to be used as a report writer, hence the name Report
Program Generator.
created the output files… that is, no read or write statements were
40
needed.
3 - Fixed column locations for values of each programming statement. That is,
a value of '0' is off and a value of '1' is on. Switches were used to
determine the results of I-O operations and needed for simple compare
The programs were sometimes rather ugly. The use of indicators made many programs very hard to
understand.
RPG has evolved nicely to look more and more like other modern languages. While indicators are still
used for Input-Output (read and write) operations, standard conditional operations are now available.
IF/THEN, DoWhile, Do Until and Select Case operands exist. While still not completely freeform, the
statements have portions that are free form. Also, powerful string handling functions like %trim, %subst,
%len and %scan are available. If you are familiar the concepts of structured programming you will see
that RPG has everything required for structured code.
So, while RPG is about 40 years old, its newest form (RPG IV) is modern and structured. The problem is
that many programs are still in use that have very old programming styles. So, you really must be familiar
with both RPG/400 (also known as RPG III) and RPG IV (also known as RPG ILE).
RPG is a full featured language that can do all functions of data processing: reading files, randomly
retrieving data, navigating through indexes, reading and writing to data terminal or display files, creating
print reports and database files.
If you are still concerned that RPG is too old to be useful, consider that Visual Basic and ASP are really
just new versions of BASIC. BASIC is as old as RPG. Also, you should know that RPG ILE can create
compiled objects that bind with modules written in other languages (like C) and can be used to process
CGI scripts on the internet and output dynamic HTML. No kidding.
If you have an RPG book, I suggest you use it in addition to (or in place of) this tutorial. It is a
challenging task to describe such a complex language in short, readable topics.
41
To proceed you need to be familiar with PDM (see topic 4) and should have completed the exercise in
topic 5 that copies a file.
Since it important to know the old way of doing things (RPG/400) and the new way (RPG/ILE), I will
present each program we write in both languages. Learn the RPG/400 style so you can get a job. Learn
the RPG/ILE so you can see how modern the language has become.
Topic 5 showed you how to copy a file named CUST into your user library. Since this will be the input
file to our program, make sure that your library list includes your user library. I prefer to have this library
as my current library.
So, use DSPLIBL to display your library list. If you don't see your user library in the list, use the
CHGCURLIB USER999 command. This will change your current library to USER999. Remember that
when I say USER999, I mean your user ID.
If this doesn't show your file, either you didn't copy the file (see Topic 5) or your library list does not
include your user library. You should see that the address for every record is "123 MAIN STREET". We
will write a program to read the first record of the file and change the address to "456 OAK". In the next
tutorial we will convert the program to RPG-ILE (RPG IV). In later topics we will change the program to
read every record in the file and change the address to "456 OAK" in every record.
We need to enter program source statements, so start PDM (STRPDM). You should see something like:
------------------------------------------------------------------------------------
---------------------------------------------------------------------------------
42
Key in 3 and hit ENTER. Fill in the screen to look like:
Member:
-------------------------------------------------------------------------------
Of course, change the USER999 to your user ID. Hit ENTER now.
If you get an error message that QRPGSRC was not found in USER999, you need to create the file
QRPGSRC to hold your RPG-III (RPG/400) source code. For a refresher on this, review the topic on
PDM. The command to create QRPGSRC in your library is:
File . . . . . . QRPGSRC
43
(No members in file)
Remember from your PDM lesson that this is trying to show you a list of all of the RPG programs that
you have entered. Since you haven't entered any, there is nothing to show.
You need to add a program. Let's call the program TUTR001. This name could be any 10 characters but if
you don't follow a naming convention you will confused quickly. This name is derived from TUT (for
tutorial), R for RPG and 001.
Hit F6 to add the program. Key in the name (the source member), RPG for the source type and a short
description. Your screen should look like:
---------------------------------------------------------------------------
-----------------------------------------------------------------------
Now hit enter and you will see a blank screen where you can enter RPG source code.
Back in the seventies, programmers had to keep track of what column each operand of the RPG statement
was in. It was a mess.
With PDM (actually SEU, the Source Entry Utility), things are easier. Not easy, but easier. The problem
is that RPG is a fixed column language. It is not a free format language. SEU provides prompting to help
you but even it is a bit complicated at first.
The problem is that the different records types (F, I, E, C, O) do multiple things. That is, the I record type
can be used several different ways so it has several different ways of prompting.
The first statement of this program is a FILE specification. That is, it will name the file that we will read
and update. Since we want to read the CUST file, we will enter an F record to define the file CUST.
To do this, hit F23. Since your PC keyboard doesn't have an F23 key, the Telnet5250 product you are
using has mapped the SHIFT-F11 key as an F23 key. So, hold down SHIFT and hit F11. The top of your
screen should look like:
44
Select Prompt
Wow. This means that there are 21 different formats to choose from. This is an easy one. Since this is a
FILE specification, we will use the F prompt. So key F for the Prompt and hit ENTER.
0001.00
File File
What a mess! A couple of things have happened. The first is that the display shows your first line of the
program as line 0001.00. Not only that, the SEU editor has highlighted it because it is blank, RPG-III
45
does not allow blank lines! The rest of the screen shows all of the fields that might be used to define a
file. Don't panic yet. All you need to enter is:
DISK for Device (2nd line from the bottom on the right)
Hit ENTER and your values will go in the right places. Amazingly, the one value that you can't enter here
is the F that tells RPG that this is a FILE specification. So hit enter, your screen looks like:
FMT F .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+
So put your cursor on the character right before CUST and key an F.
FMT F .....FFilenameIPEAF....RlenLK1AIOvKlocEDevice+
Now, calm down. This may be the only time in your life that you actually key in a FILE specification. In
the world of programming, everything is copied and changed, cut and paste. Normally, you will simple
copy an existing program and then change and add statements to it.
So, what did you just define? You told the program that you will be using the file CUST, that the file is
being used for Input (you will read the file), that the file is Fixed length (almost all files are fixed length),
that the file is Externally defined (that is, it was defined by the database language of the AS/400) and
finally that the file is a DISK file (not a PRINTER or Display Screen).
Imagine for a moment a program where the user enters a customer number on the display screen. The
program reads the customer record, looks up the customer's most recent order and prints it. Such a
program will have at least 4 files: Display screen file (a WORKSTN file) Customer file (a DISK file)
Order file (a DISK file) Printer file (a PRINTER file)
46
This is a simple program. It has only one file.
Now we are ready for the processing logic of the program. The total logic of this program will be only 6
statements. Processing logic in RPG is known as CALCULATION statements and uses the C
specification and the C prompt type.
Soon, you will get more comfortable with PDM and SEU. Now, we need to add a line to after the F
statement. Do this by keying an I (for insert) on top of the 0001.00.
FMT FX .....FFilenameIPEAF........L..I........Device+
When you hit ENTER, the editor will create a blank line for you and even place your cursor in column 6
where you can enter the C for CALCULATION. With the cursor on the line with the C, hit Shift-F11 to
prompt and this time use C for the prompt type.
Decimal
Now finally some good news. Most of the RPG programming you do is using the C specification. This
type of statement is not too messy.
The first processing step of the program is to read the first record of the CUST
2) what indicator to turn on if there was not a "next" record in the file
(that is, what indicator will tell you that there is no more data in
the file)
Indicators are numbered from 01 to 99. There are some other special ones too.
47
I like to use indicators 90 thru 99 for the read statements. So enter
90 for EQ
0002.00 C
'''''''
READ CUST
Decimal
90
FMT FX .....FFilenameIPEAF........L..I........Device+......KExit++En
48
0002.00 C READ CUST 90
There are 3 places to enter indicators for statements. Unfortunately, it is not always easy to figure out
which place to use. The HI, LO and EQ headings come from the obsolete way indicators were first used.
In old RPG, there were no IF statements. Instead, you used a COMP (Compare) statement to compare the
values of FACTOR1 and FACTOR2 and then would set on an indicator if FACTOR1 was higher than
FACTOR2, a different indicator if FACTOR1 was lower that FACTOR2 and finally an indicator if they
were equal. We will NEVER do this.
The lesson here is simple. For a READ instruction, the indicator mentioned in the EQ field will be turned
on when there are no more records in the file.
Let's pause for a minute and look at all that is happening with these 2 statements.
The F specification tells the AS400 to set up a way to read the CUST file. It also tells it to find the fields
and attributes of the fields using the database definition of the CUST file. In most other languages, you
would need a statement to OPEN the file. Such a statement tells the computer that you are ready to use
the file. RPG assumes that you want to read the file and automatically opens the file for you. There is a
simple way to tell the AS400 that you want to OPEN and CLOSE the file explicitly but there is no reason
to do that.
The READ statement reads the first record of the file CUST and populates an area in your program's
memory with the data that is in the first field. You program has each field defined and now has data in
each field.
After the READ, the field CSNBR has a value of 1002, the field CSNAME has a value of E LUMPKIN,
the field CSADR1 has a value of 123 MAIN STREET.
We now want to change the value in CSADR1. Unlike other languages, if we move only a few characters
to a field that is 30 characters long, RPG will change only the first 3 characters. So we will first blank out
the field and then move 456 OAK to the address.
Insert a line for a new C statement. One way to do this is to key an I over the
0002.00 and hit ENTER. Then key C in column 6 and hit Shift-F11. Key C for prompt
type and hit ENTER. We want to blank out the CSADR1 field so we will move blanks
to it. RPG has some system values defined that are named like:
*BLANK or *BLANKS
*ZERO or *ZEROES
*ON or *OFF
As you might guess, we want to move the value *BLANK to CSADR1. So fill in your screen to look like:
49
Prompt type . . . C Sequence number . . . 0003.00
Decimal
Now insert another line to MOVEL '456 OAK' to CSADR1. This says to move the characters
in between the quotes to the field named CSADR1. The operand is MOVEL for MOVE LEFT.
This means that since '456 OAK' is only 7 characters and CSADR1 is 30 characters,
50
At this moment, the data on the disk has the value '123 MAIN STREET' in the area of
the record for CSADR1. If you ran this program, at this spot in the program, the
value of the field CSADR1 in memory would be '123 OAK'. But if you stopped here,
the new value ('123 OAK') would not get stored in the file. To do that you must
UPDATE the record. In RPG-III verbs or operands can be only 5 characters so the
verb is UPDAT. Also, you must use the record name for the file. This is too
complicated to explain here but trust me that the record name for the CUST file
is CSREC.
So add the statement to update the record CSREC. Now that we see that we are using
the file for Update (not just Input), it is time to change the I in the FILE
definition from I to U.
0005.00 C UPDATCSREC
First, we have to discuss a housekeeping item left over from the very early days
of RPG. We know that RPG will automatically open the file. However, it will only
close the file if the "LAST RECORD" indicator is ON. For now, just make sure the
RETRN
51
So the final program is:
0005.00 C UPDATCSREC
0007.00 C RETRN
Now that you are finished, hit F3 and ENTER to save your statements.
So you now have the source statements to compile an RPG program. You compile it just like you
compiled the CL program. Key a 14 on the line next to the program name in PDM and hit ENTER. Like:
File . . . . . . QRPGSRC
By the way, this program is in the USER000 library. You can see it using PDM.
After compiling your program, you will want to look at the compiled listing. Remember, you do this with
the command:
WRKSPLF
52
Work with All Spooled Files
Device or Total
Put your cursor next to the last one and key in a 5 and hit ENTER.
You will see your compiler listing. If you key in a B at the top and hit ENTER, the AS400 will take you
to the bottom of your listing. If your compile worked, it will look like:
F i n a l S u m m a r y
TOTAL 00 10 20 30 40 50
2 2 0 0 0 0 0
Records . . . . . . . . . . : 7
Specifications . . . . . . : 7
Table Records . . . . . . . : 0
Comments . . . . . . . . . : 0
* * * * * E N D O F C O M P I L A T I O N * * * *
The phrase "Program TUTR001 is placed in library USER???" means the AS400 understood your
statements well enough to crate an executable program.
If your compile didn't work, start by making sure that you have your user library in your library list. The
easy way to do this is:
53
CHGCURLIB USER999
All that is left is to run the program. There are several ways to do this but the most straightforward way is
to use this command:
CALL TUTR001
The program will run almost instantly. Then if you look at the CUST file the address of the first record
will have changed:
RUNQRY QRYFILE(CUST)
Boy what a long topic! Each lesson will build on this program. It is important that you get familiar with
entering programs, compiling them and looking at the compile listings.
You will see a list of reports in your spool file. It will look like:
Device or Total
Each line is the name of a report on your Spool File. The most recent reports are at the bottom. Key a 5 next the report you want
to view.
You should now be looking at the compiler listing. To quickly locate errors, key B (for bottom) in the CONTROL field and hit
ENTER. You are now looking at the bottom of the compiler report.
If your program compiled successfully, the last few lines will look
like:
* * * * * E N D O F F I N A L S U M M A R Y * * *
*
54
Program CMNR020 placed in library AIR. 00 highest severity. Created on
* * * * * E N D O F C O M P I L A T I O N * * * * *
* * * * * E N D O F F I N A L S U M M A R Y
* * * * * E N D O F C O M P I L A T I O N * *
To find the errors, hit the PAGE UP key until you see:
* * * * * E N D O F E X T E R N A L R E F E R E N C E S *
*
M e s s a g e S u m m a r y
* * * * * E N D O F M E S S A G E S U M M A R Y * * *
*
These are the errors that need correcting. Anything with severity level 00 is NOT an error.
To find the error, go to the top of the report. Do this by keying T in the CONTROL field and hitting ENTER.
Then, key the error message ID in the FIND field. In this case, I keyed RNF7030 in the FIND field. Then, hit F16 to search.
Remember F16 is SHIFT / F4.
55
*RNF7503 30 123 005200 Expression contains an operand tha
The errors RMF7030 and RNF7503 need to be fixed. They are both related to sequence # 123 in the compile listing which is line
number 52.00 in the source file.
In my case, I misspelled a variable name. This caused 2 errors. Often, 1 error will cause more than 1 error message.
Hopefully you were able to complete the previous topic and compile a simple RPG III program. Though
introduced in 1994, RPG IV is known as the "new" RPG. Because of the huge number of programs
written in RPG III, the industry has been slow to adopt RPG IV. It is undoubtedly the way of the future on
the AS/400.
RPG III is more accurately known as RPG/400 when it runs on an AS/400. RPG IV supports the
"Integrated Language Environment" concept introduced in 1994. This is known as ILE and allows
program modules to be bound together at compile time so that an RPG IV program can use routines
written in C or any other language. So RPG III is essentially the same as RPG/400 and RPG IV is
essentially the same as RPG ILE.
Now you are emotionally ready to convert your program TUTR001 to RPG IV.
First you must create a place for your program statements. Since RPG IV statements are longer than other
program source statements, the command to create the source physical file is slightly different. To create
the appropriately named place for RPG IV source:
Of course, change the USER999 to your user ID. This command creates a source physical file named
QRPGLESRC to hold your programs written in RPG IV. The name is the AS/400 standard for RPG IV or
RPG ILE source code. It starts with "Q" followed by RPGLE (short for RPG ILE) and then SRC for
source.
Now, use the AS400 command to convert the old program to RPG ILE. Key CVTRPGSRC (Convert
RPG Source) and hit F4 to see the prompts. Your screen should look like:
56
To file . . . . . . . . . . . . QRPGLESRC Name, *NONE, QRPGLESRC
Now fill in the values to indicate that you want to convert the program located in source physical file
QRPGSRC in library USER999 that is named TUTR001 and that you want the converted program to be
in the same library with the name TUTR002.
Hit ENTER and the command will copy and modify your program.
You can now use PDM to look at the converted program. From the PGM screen key in 3 and hit ENTER.
Then fill in the screen to look like:
Member:
57
Type . . . . . . . . . *ALL *ALL
Hit enter and you will see your converted program in the list of programs:
File . . . . . . QRPGLESRC
Notice that the "Type" is RPGLE. That tells the AS400 to compile this as RPG ILE instead of RPG III.
Key a 2 next to the program name (TUTR002) and hit ENTER and you will see the source code:
0007.00 C RETURN
You can see that not much has changed. RPG IV has more room for operand names so the UPDAT and
RETRN are now spelled out. The other values are spread out a bit more.
You can compile and run this program. You should try that now. Remember that you compile by using
option 14. You can then CALL TUTR002 to run it.
58
Now copy this program and modify it to reflect the new coding style of RPG IV. Copy the program using
PDM. Simply key 3 as the option:
File . . . . . . QRPGLESRC
Copy Members
Type the file name and library name to receive the copied members.
To library . . . . . USER000
59
Member New Name
TUTR002 TUTR003
Now hit ENTER again and PDM will copy TUTR002 and name it TUTR003.
One way RPG IV is different is that lower case letters are allowed. This doesn't change the functionality
of the program but it certainly makes it feel more modern.
You must tell SEU that you want to use lower case letters. When you are editing the TUTR003 program,
hit F13 (well actually Shift-F1) and you will get the options for the editor:
C=Cursor, D=Data
1-999
Syntax checking:
60
It is best to change the first value to C. This affects how the screen scrolls when you PAGE-UP and
PAGE-DOWN. Make sure the "Uppercase input only" selection is N. This allows you to key lowercase
characters.
For now, make 2 changes in the style. First, use lower case characters when you want. Second, use the
EVAL operand in place of MOVE and MOVEL. EVAL is short for EVALUATE and like the old BASIC
"LET". It is not only more like other modern languages, it has other enhancements that make string
handling easy.
Also, since there is more room, change the address to 456 OAK STREET.
0007.00 C Return
Since the EVAL instruction works a little differently from the MOVE, it is no longer necessary to move
blanks to CSADR1 before moving 456 OAK STREET to it.
Now compile and run the program. After running TUTR003 you can look at the file with:
RUNQRY QRYFILE(CUST)
and see that the address has changed to 456 OAK STREET.
You should be getting more comfortable with PDM, SEU, compiling programs and looking at spool files.
If so, you are on your way to being a productive programmer.
In a previous topic, you should have written a very short RPG III program to change the first record in the
CUST file.
Let's add so more statements to the program so that it will read all records in the file and change the zip
code for everyone in Tennessee.
You may want to read this very short article on data navigation first:
Data Navigation
Your program will still reference the CUST file, so, use DSPLIBL to display your library list. If you don't
see your user library in the list, use the CHGCURLIB USER999 command. This will change your current
library to USER999. Remember that when I say USER999, I mean your user ID.
61
You can look at your data file with the command: RUNQRY QRYFILE(CUST)
Now, use PDM to copy your old program. Start PDM (STRPDM). You should see something like:
---------------------------------------------------------------------
---------------------------------------------------------------------
Member:
62
Type . . . . . . . . . *ALL *ALL, type, *generic*,
-----------------------------------------------------------------------
Of course, change the USER999 to your user ID. Hit ENTER now.
File . . . . . . QRPGSRC
Key a 3 as the option next to TUTR001. Hit ENTER and then fill in the screen to copy the program and
name it TUTR004:
Copy Members
Type the file name and library name to receive the copied members
To library . . . . . USER999
63
Member New Name
TUTR001 TUTR004
Hit ETER and you should now see the copied program in your list. You may want to change the
description of the program so you'll remember which program does what. Your screen should look like:
Remember from your PDM lesson that this is trying to show you a list of all of the RPG
File . . . . . . QRPGSRC
Key a 2 next to TUTR004 so you can change the source statements to the program.
Before we make the changes to the program we need to discuss different ways to write a loop. We want a
loop that will read every record in the CUST file and change the records that have TN (Tennessee) as the
state.
change it
update it
Remember that when RPG III reads a record, you must specify which indicator to turn on it there are no
more records; that is, it is END OF FILE.
64
You could write the instructions above in RPG III like this:
FCUST UF E DISK
C READIT TAG
C READ CUST 90
C UPDATCSREC
C GOTO READIT
C ENDIF
C RETRN
Then, name the first line of the Calculations READIT. The, try to read a record (if END OF FILE, move
ON to indicator# 90) If indicator# 90 is *OFF Move … Move … Update … Go back to the first line of
the program Turn on the "magic" LR indicator End the program
This will work. The only problem is that for about 15 years, it has been considered bad style to use
GOTO statements. This is an industry trend that includes all languages and all computers.
The reason is that long programs are hard to understand when there are a lot of GOTO statements.
Programs with lots of GOTO's are nicknamed spaghetti programs because the statements can be as hard
to follow as a strand of spaghetti.
SO, the industry prefers that we use DO statements. We can use a DO WHILE or a DO UNTIL. This still
leaves us with 2 good ways to write the loop. I prefer the first because it looks a lot like the GOTO loop
but without the GOTO:
FCUST UF E DISK
C *IN90 DOWEQ*OFF
C READ CUST 90
65
C UPDATCSREC
C ENDIF
C ENDDO
C RETRN
The DOWEQ statement says that as long as indicator # 90 is OFF, execute all of the statements until the
ENDDO (End DO) statement.
Then, start back at the top (a lot like a GOTO). If indicator # 90 is still OFF, execute the statements again.
Eventually, the program will read all 1,000+ records in the file. When it does, indicator # 90 will be
turned ON.
When indicator # 90 is ON, the program will not update the record and it will not continue to loop.
The other way to code the loop is with what is called a priming read statement. This method needs 2
READ statements but does not need the IF statement.
If looks like:
FCUST UF E DISK
C READ CUST 90
C *IN90 DOWEQ*OFF
C UPDATCSREC
C READ CUST 90
C ENDDO
C RETRN
Here, the first record is read before the DO begins. The statements are executed and the READ statement
is at the end of the DO loop.
Using the first acceptable loop, add the IF statement to test to see if the state is TN. Also, replace the code
so that it changes the ZIP to 987650000.
66
C CSSTE IFEQ 'TN'
C UPDATCSREC
C ENDIF
FCUST UF E DISK
C *IN90 DOWEQ*OFF
C READ CUST 90
C UPDATCSREC
C ENDIF
C ENDIF
C ENDDO
C RETRN
This is considered good code by most programmers. But it really helps to move the update logic to a
separate place in the program as a subroutine. This is done here only to help readability. It doesn't change
the outcome.
In RPG, subroutines begin with a BEGSR (BEGIN SUBROUTINE) statement which also has the name
of the subroutine. They end with ENDSR (END SUBROUTINE). They are run with the EXSR
(EXECUTE SUBROUTINE) command.
If we move the update logic to a subroutine named UPD, the program looks cleaner. Also, we can add
"COMMENTS" records. Any statement with a * after the record type, is a "COMMENT". In fact you can
even remove the record type (the C or F in column 6).
With a few comment lines added to define the sections of the program, we get a pretty readable program:
FCUST UF E DISK
67
*-----------------------------------------------------
C *IN90 DOWEQ*OFF
C*
C READ CUST 90
C EXSR UPD
C ENDIF
C ENDDO
C*
C RETRN
*-----------------------------------------------------
C UPD BEGSR
C*
C UPDATCSREC
C ENDIF
C*
C ENDSR
*------------------------------------------------------
Now, compile this (remember… option 14) and run it (CALL TUTR004).
Then use RUNQRY QRYFILE(CUST) to see the data. If you scroll right by hitting F20 (actually Shift
and F8) you will see that you changed the zip in all of the records with TN as CSSTE.
In the previous topic, we added a DO loop to read all records in the file, an IF statement to test the state
for "TN" and a subroutine.
You should be beginning to sense that RPG III is powerful but ugly.
Let's recreate this program using the easier to read syntax of RPG IV. The simplest way to do this is to
copy TUTR003 in the QRPGLESRC source object and then modify the copied program.
Use PDM to copy TUTR003. Start PDM (STRPDM). You should see something like:
68
--------------------------------------------------------------------------------------
----
--------------------------------------------------------------------------------------
----
Member:
69
Of course, change the USER999 to your user ID. Hit ENTER now.
File . . . . . . QRPGLESRC
Key a 3 as the option next to TUTR003. Hit ENTER and then fill in the screen to copy the program and
name it TUTR005:
Copy Members
Type the file name and library name to receive the copied members
To library . . . . . USER999
70
Member New Name
TUTR003 TUTR005
Hit ENTER and you should now see the copied program in your list. You may want to change the
description of the program so you'll remember which program does what.
Key a 2 next to TUTR005 so you can change the source statements to the program.
The old syntax on the IF and DO statements is legal. So you can choose between:
AND
Not only is the second form more readable, it is more free form. You can put spaces and parentheses in
the conditional part of the statement like this:
This may not look like such a big deal, but compare this old style IF statement:
C (CITY = 'ATHENS')
The newer style is not only more readable but you can actually figure out how the conditions are checked.
71
One of the other big changes in form from RPG III to RPG IV is that the subroutine names can be only 6
characters in RPG III. In RPG IV, the subroutine names can be 14 characters. These longer names make
the programs many times easier to follow.
So using this new found knowledge about RPG IV, change the program to look like:
FCUST UF E disk
C*---------------------------------------------------------------------
C Read CUST 90
C If *in90 = *off
C Exsr UpdateCSREC
C EndIf
C EndDo
C Return
C*---------------------------------------------------------------------
C UpdateCSREC BegSR
C If CSSTE = 'TN'
C Update CSREC
C EndIf
C EndSR
If you really hate using the indicators - and you probably do - you can use one of the new built in
functions to check to see if the last record has been read. That is, you are checking to see if the file is at
END OF FILE or EOF.
72
C DoW not %eof(CUST)
C Read CUST
C If not %eof(CUST)
C Exsr UpdateCSREC
C EndIf
C EndDo
In fact, the %EOF function will refer to the last file used by default. So you could write:
C Read CUST
C If not %eof
C Exsr UpdateCSREC
C EndIf
C EndDo
If you have ever used Visual Basic, the EOF function will look familiar.
That's it!
You are beginning to see how to create the different pieces of a complete system. You have written
queries that display and print data, you know how to view printed data and you have written several small
programs that can change data. After this topic, you will learn to write programs to display and change
data interactively - data maintenance, entry, update and display. In this topic, you will use yet another
three letter acronym, SDA, as a tool. You will also make a couple of changes to your user profile. The
end result will be a user interface that you could use as an entry point to an application system. The user
would sign on to the AS/400 and immediately see the user menu. The user would never see the AS/400
programmer and operator menus. In summary, here is the task:
1) Create a menu named STARTMEN that will let the user:
Run the program to change the address line in the CUST file
Sign Off
73
2) Change your user profile so that when you sign on, you see this new menu. One of
the
most intuitive development tools on the AS/400 is SDA, Screen Design Aid. With it, you
build display screens and menus in a WYSIWYG (what you see is what you get) mode.
We're
Start SDA with the command STRSDA. You will see that SDA has three functions:
1) Design Screens
2) Design Menus
You'll want to experiment with each of these later. For now, use selection 2 to design a menu. Enter the
name of your new menu, where you want to store the source (usually QDDSSRC) and the library that it
will be in. SDA will look for that menu and since it can't find it, it will create it.
Design Menus
Of course, replace USER999 with your user library name. When you hit ENTER you will see:
Library . . . . : USER999
74
Type choices, press Enter.
The AS/400 even fills in the "Y" to indicate that you want to work with menu images and commands.
Hit ENTER again and you see the following template menu.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Selection or command
SDA creates a template for you that has the menu name and a list of ten selections. On this screen, you
list the selections for the user. On a separate screen, you list the commands associated with each selection.
Avoid keying in "insert" mode since that will shift all text on the screen. To remove text, just space over
it.
75
STARTMEN STARTMEN Menu
6.
7.
8.
9.
When you hit enter, SDA uses your screen to build literals in a display file. F20 (Shift and F8) provides a
way to display the text blocks that SDA will use. Each text block is preceded by a hidden character that
contains the display attribute for that string of text. If you key an "H" right in front of a block of text,
SDA will display that text in high intensity or white characters. An "R" will display the text in reverse
image. "D" will delete the text.
Next, define the commands for each selection. Hit F10 to enter the commands. You now can key in the
commands for selections 1 through 99. After hitting F10, you should see:
Option Command
01
02
76
03
04
05
06
07
Here, you enter the commands associated with each selection. So enter the commands like this:
Option Command
01 runqry csq001
02 runqry csq002
03 call tutr005
04 RUNQRY QRYFILE(CUST)
05 wrksplf
77
06
07
Then, hit page down so you can enter SIGNOFF as the command for selection 10. You can enter any
command that you want the user to run. You can run queries, submit print queries to run in batch and use
standard utility commands such as "Work with Spool Files", WRKSPLF. Finally, you can help the user
navigate to another menu with the "GO" command used for selection 6. Hit F3 a few times to exit. When
you exit SDA, check the "object library" name. This is where your compiled menu objects will go.
F4 for list
Text . . . . . . . . . . . . . . . .
78
Replace menu objects . . . . . . . . Y Y=Yes, N=No
Wait! Let me say again - the value near the bottom of the screen - Object Library - must be your user
library. Otherwise you will create your menu somewhere else. Hit enter and the AS/400 will compile your
source code and your menu will be ready to run. SDA creates three objects, all with the same name: the
commands, the display file and a message file for the menu. If you want some added fun, go back to the
menu source using SDA and this time make the selection to "work with menu help". Here you can define
help screens that will display if your user enters the selection number and hits F1. Now you can tie this
menu to your new employee's user profile. Use the command to change your profile,CHGPRF. From a
command line key CHGPRF and hit F4.
When you sign on to the AS/400, these values tell the operator what your current library should be and
what menu is your default menu. Presently, your current library is probably *CRTDFT which is a system
value that means "NONE". Your initial menu is named MAIN. This is the menu you have gotten
accustomed to seeing when you sign on. Change your current library from *CRTDFT to your user library.
Also change your initial menu to STARTMEN. Make the LIBRARY value under your initial menu your
user library. Your screen should look like:
79
Initial program to call . . . . SOC000 Name, *SAME, *NONE
If you sign off and sign back on, you will see your new menu. By the way, if you set it up wrong, you
may have to e-mail me to reset your profile. For example, if you changed your profile to run the menu
ABCDEF as the initial menu, the AS/400 won't know what to do (since there is no ABCDEF menu). If
you want to see the familiar sign on screen, key in the command GO MAIN. It's that easy to customize
the screen selections for a user.
One of the most intuitive development tools on the AS/400 is SDA, Screen Design Aid. With it, you build
display screens and menus in a WYSIWYG (what you see is what you get) mode. We're going to look at
how easy it is to create a simple help screen.
In only a few minutes, you can build a custom help screen for your user. Start SDA with the command
STRSDA. You will see that SDA has three functions:
1) Design Screens
2) Design Menus
3) Test Display Files
Use selection 2 to design a menu. Enter the name of your menu, where you have stored the source
(usually QDDSSRC) and the library that it is in.
Now choose the selection to "Work with menu help" by changing the "N" to a "Y". Here you can define
help screens that will display if your user enters a selection number and hits F1.
This is where you will "Create" the text for your help screen. You will also have 3 Range Options to
choose from.
the menu.
80
Note: You cannot create more than a single page of help for each
range of options.
You will see this screen displayed. Type in a 1 for Create in the Opt field, and 01 - 01 in the Range field.
The 01 is only used for Selection 1 of your main menu selection. Option 02 is for Selection 2, etc..
Library . . . . : User999
___ __ - __
Bottom
81
File . . . . . . : QDDSSRC Menu . . . . . . . :
MRCM001
Library . . . . : USER999
-OR-
-OR-
From here, you will press Enter, a blank screen will appear and now
you can type in the text
Once you have created the text help for each option you chose, you
should see the following
82
screen with your enhancements:
Library . . . . : User999
Bottom
When you exit SDA, check the "object library" name. This is where your compiled menu objects will go.
Hit enter and the AS/400 will compile your source code and your menu will be ready to run. SDA creates
three objects, all with the same name: the commands, the display file and a message file for the menu.
83
When you run the menu you have created, type in selection 1 and press F1, when you do this you will see
the following help screen.
this query will show you the Customer Name, Customer Number
and Address.
F3=Exit F12=Cancel
You can also use SDA to test and design display files. With the "Test Display Files" selection of SDA,
you can see the fields and presentation of a display file. This works even if you don't have the source
code.
There are a couple of ways to search through source code for information you are looking for (ie. program
names, files, attributes, etc.). The 1st way is to do a search on a single or multi-program file level through
84
the work with members option, in which you search only specific program(s) for your information. The
other way is to do a search through an entire object such as QRPGLESRC or QDDSSRC.
In using the 1st method, start the Program Development Manager (STRPDM), and use option 3 to 'Work
with members'.
Selection or command
Now type in the needed information for the file and library to which
you want to
85
Specify Members to Work With
Member:
From here, type in 25 in the Opt space of the Member you would like to
do the search on.
If you would like to check all the programs listed on this members
screen, simple hit
F13 (Shift F1) and you will see that 25 is filled in on all the
options.
File . . . . . . QRPGLESRC
86
8=Display description 9=Save 13=Change text 14=Compile 15=Create
module..
More...
Parameters or command
===>
Now hit Enter, and you will see the Find String screen. In the blank
to the right of "Find",
type in the information you are seeking and press enter. In this
example we are looking for
Find String
Find . . . . . . . . . . . . . . ABP001___________________
87
From column number . . . . . . 1 1 - *RCDLEN
Parameters . . . . . . . . . . .
F18=Change defaults
You can see now that the search found the PF in program ABC102. Hit
Enter to continue the
search, and it will look through all the files you have chosen and
will pop up the screen
Columns . . . : 6 76 Edit
USER999/QRPGLESRC
SEU==>
ABC102
88
FMT FX FFilename++IPEASF.....L.....A.Device+.Keywords++++++++++++++++
+++++++++
0003.00
*---------------------------------------------------------------------
0011.00 C EndIf
0012.00 C EndDo
0014.00 C Return
0015.00
*---------------------------------------------------------------------
0016.00
*---------------------------------------------------------------------
89
you work with objects. Once again, type in your Library name, and for
Object name, type in Q*.
This will list all of the Q objects, such as QDDSSRC, QRPGSRC, etc...
Object:
*BLANK
The next screen you see will contain the specified objects. Now all
you need to do is type
in 25 in the object you want to do the search in, and it will search
through all files/programs
Position to type . . . . .
90
2=Change 3=Copy 4=Delete 5=Display 7=Rename
Bottom
Parameters or command
===>
Now hit Enter, and the search will begin through all the members
associated with the object you
have chosen to search. You will notice the same result as above was
located in the search.
PDM.
91
There are 3 ways that reports get printed using RPG.
The oldest and obsolete method uses the RPG cycle and is not covered in this tutorial.
The next and most widely used is covered in this topic. The report is formatted using Output Specs. These
are identified with the letter O. Be sure to use F4 when entering O specs because they have many options.
An O spec is written by using the EXCEPT operation. This comes from the history of RPG. When using
the RPG cycle, the O Specs are automatically written. When you don't use the RPG cycle, the O Specs are
written as an EXCEPTion to the cycle.
The F spec for the printer file QPRINT identifies an indicator which the AS/400 turns *ON when the
page is full.
The third way of printing will be covered in the next topic. It uses a printer file defined using DDS specs
This is the RPG program for writing a Print Report using O Specs. At the bottom, you will see a sample
of the report layout in the spool file.
Columns . . . : 1 71 Edit
USER000/QRPGSRC
SEU==>
TUTR005
FMT FX .....FFilenameIPEAF........L..I........Device+......KExit+
+Entry+A....U
0005.00 * is full.
0007.00
*----------------------------------------------------------------
0009.00 C EXCPTHEADS
92
0010.00 C* Read the first record
0012.00 C*
0016.00 C EXCPTHEADS
0018.00 C ENDIF
0020.00 C EXCPTDETAIL
0022.00 C ENDDO
0023.00 C*
0025.00 C RETRN
0026.00
*----------------------------------------------------------------
0028.00 O 6 'PAGE'
0030.00 O PAGE 10
0032.00 O 65 'DATE'
0034.00 C* The edit code Y will edit the date like xx/xx/xx
0035.00 O UDATE Y 75
0036.00 O*
93
0037.00 O E 1 HEADS
0040.00 O*
0041.00 O E 1 HEADS
0043.00 O 23 'NAME'
0044.00 O 61 'ORDERS'
0045.00 O 70 'ORDERS'
0046.00 O*
0047.00 O E 1 DETAIL
0048.00 O CSNBR 17
0049.00 O CSNAME 49
0050.00 C* The edit code 1 will edit the amount like 1,234.50
0051.00 O CS#OPN1 60
0052.00 O CS$OPN1 70
Once you have written the program, compile it using option 14. After
you have compiled the
program, you want to call it and see what the report looks like. To
call the program, simply
Next, go to the Work Spool File (WRKSPLF), find the program, and key
in a 5 to view the report.
94
PAGE 1 CUSTOMER ORDER REPORT DATE
11/14/02
# OPEN AMT
OPEN
001002 E LUMPKIN 0 .
00
001084 CHISOLM 0 .
00
001105 HAGGINS 0 .
00
001109 BYRD 0 .
00
001168 ROMERO 0 .
00
001177 POUNDS 0 .
00
001183 HINTON 0 .
00
001184 YANCEY 0 .
00
001186 MCDOUGLE 0 .
00
001202 DAVIS 0 .
00
001208 TARVIN 0 .
00
001214 GOOSEBERRY 0 .
00
001234 REID 0 .
00
001239 COLBERT 0 .
00
001259 BLACKMON 0 .
00
95
001264 CRANFORD 0 .
00
001266 DODSON 0 .
00
001269 LENSON 0 .
00
001274 JACKSON 0 .
00
001284 TAPER 0 .
00
001287 WILLIAMS 0 .
00
001293 FEAZELL 0 .
00
001294 MC CULLUM 0 .
00
001301 SMITH 0 .
00
001303 MCGHEE 0 .
00
001308 WALLACE 0 .
00
001318 ROBINSON 0 .
00
001321 SPIVEY 0 .
00
001322 HILLARD 0 .
00
# OPEN AMT
OPEN
001480 BAILEY 0 .
00
96
001485 POLLARD 0 .
00
001492 JORDAN 0 .
00
001502 EVERETT 0 .
00
001504 HIGGINS 0 .
00
001509 JONES 0 .
00
001523 BENDER 0 .
00
001539 BUDD 0 .
00
001557 KINSEY 0 .
00
001561 PILKINGTON 0 .
00
001579 HAWKINS 0 .
00
001583 MOBBS 0 .
00
001584 SELLERS 0 .
00
001587 WALKER 0 .
00
001591 SLACK 0 .
00
001604 HINKLE 0 .
00
001606 ALLEN 0 .
00
This is the RPG-ILE program for writing a Print Report using O Specs. At the bottom, you will see a
sample of the report layout in the spool file.
97
Columns . . . : 6 76 Edit
USER000/QRPGLESRC
SEU==>
TUTR006
FMT FX FFilename++IPEASF.....L.....A.Device+.Keywords++++++++++++++++
+++++++++
0004.00 * That means that *IN10 will be *ON when the printed
page
0005.00 * is full.
0007.00
*----------------------------------------------------------------
0012.00 C*
0018.00 C EndIf
98
0020.00 C Read CSREC
90
0021.00 C EndDo
0022.00 C*
0024.00 C Return
0025.00
*----------------------------------------------------------------
0027.00 O 6 'PAGE'
0028.00 C* PAGE is a system value that will keep track of the page#
0029.00 O Page 10
0031.00 O 65 'DATE'
0033.00 C* The edit code Y will edit the date like xx/xx/xx
0034.00 O Udate Y 75
0035.00 O*
0036.00 O E Headings 1
0039.00 O*
0040.00 O E Headings 1
0042.00 O 23 'NAME'
0043.00 O 61 'ORDERS'
0044.00 O 70 'ORDERS'
0045.00 O*
0046.00 O E Detail 1
99
0047.00 O CSNBR 17
0048.00 O CSNAME 49
0049.00 C* The edit code 1 will edit the amount like 1,234.50
0050.00 O CS#OPN 1 60
0051.00 O CS$OPN 1 70
Once you have written the program, compile it using option 14. After
you have compiled the
program, you want to call it and see what the report looks like. To
call the program, simply
Next, go to the Work Spool File (WRKSPLF), find the program, and key
in a 5 to view the report.
# OPEN AMT
OPEN
001002 E LUMPKIN 0 .
00
001084 CHISOLM 0 .
00
001105 HAGGINS 0 .
00
001109 BYRD 0 .
00
100
001168 ROMERO 0 .
00
001177 POUNDS 0 .
00
001183 HINTON 0 .
00
001184 YANCEY 0 .
00
001186 MCDOUGLE 0 .
00
001202 DAVIS 0 .
00
001208 TARVIN 0 .
00
001214 GOOSEBERRY 0 .
00
001234 REID 0 .
00
001239 COLBERT 0 .
00
001259 BLACKMON 0 .
00
001264 CRANFORD 0 .
00
001266 DODSON 0 .
00
001269 LENSON 0 .
00
001274 JACKSON 0 .
00
001284 TAPER 0 .
00
001287 WILLIAMS 0 .
00
001293 FEAZELL 0 .
00
101
001294 MC CULLUM 0 .
00
001301 SMITH 0 .
00
001303 MCGHEE 0 .
00
001308 WALLACE 0 .
00
001318 ROBINSON 0 .
00
001321 SPIVEY 0 .
00
001322 HILLARD 0 .
00
# OPEN AMT
OPEN
001480 BAILEY 0 .
00
001485 POLLARD 0 .
00
001492 JORDAN 0 .
00
001502 EVERETT 0 .
00
001504 HIGGINS 0 .
00
001509 JONES 0 .
00
001523 BENDER 0 .
00
001539 BUDD 0 .
00
102
001557 KINSEY 0 .
00
001561 PILKINGTON 0 .
00
001579 HAWKINS 0 .
00
001583 MOBBS 0 .
00
001584 SELLERS 0 .
00
001587 WALKER 0 .
00
001591 SLACK 0 .
00
001604 HINKLE 0 .
00
001606 ALLEN 0 .
00
As mentioned in the previous topic, there are 3 ways that reports get printed using RPG.
This topic will look at how to print using a print file defined with DDS specs.
In an earlier topic, we saw how to use DDS to define a physical file and a logical file. DDS is also used to
define print files and display files. A future topic will discuss display files.
A display and print file are different from a physical file in two ways:
1) A physical file can have only 1 record definition.
2) A print file and a display file are really defining different records, not data. I think of print files as a
buffer in memory that describes a print line.
To create the print file, STRPDM and add a member to QDDSSRC in your library. Make sure the TYPE
is PRTF.
Add a member named CUSW007. Enter the specs shown in the DDS attachment. Or if you are lazy, copy
it from library USER000.
Columns . . . : 1 71 Edit
USER000/QDDSSRC
SEU==>
CUSW007
103
FMT A* .....A*. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+...
6 ...+... 7
0001.00 A* THE NEXT LINE TELLS THE COMPILER TO LOOK IN THIS FILE
0003.00 A REF(CUST)
0004.00 A* SKIPB(1) MEANS SKIP TO THE TOP OF THE NEXT PAGE BEFORE
0006.00 A R HDG1
0007.00 A SKIPB(1)
0008.00 A 2DATE(*YY)
0010.00 A 60'PAGE'
0011.00 A 67PAGNBR
0013.00 A SPACEA(2)
0014.00
A*----------------------------------------------------------------
0015.00 A R HDG2
0016.00 A SPACEA(1)
0019.00
A*----------------------------------------------------------------
0020.00 A R HDG3
0021.00 A SPACEA(1)
104
0024.00 A 48'ORDERS'
0025.00 A 60'ORDERS'
0026.00 A SPACEA(2)
0027.00
A*----------------------------------------------------------------
0028.00 A R DETAIL
0029.00 A SPACEA(001)
0030.00 A CSNBR 6S 0O 2
0037.00
A*----------------------------------------------------------------
0038.00 A R TOTAL
0039.00 A SPACEA(001)
0040.00 A 30'TOTAL'
I have chosen to use the same field names in the print file as the input file that I will read. I could have
used different field names, but RPG handles fields with identical names in an interesting way.
So, since we use the same values in the print file as the input, we need only read the input file and write
the print records. As before, we still need to take care of headings and page control.
To show you different techniques, I stated the length and type of CSNBR and CSNAME in the DDS. For
the other fields, I put in R in the REF column. This tells the compiler to look for the field with this name
in the file named at the top in the REF option. It gets the field type and length from the file CUST since
105
that is the file named in the REF parameter. Defined fields using the REF technique are easier and
preferred.
If you are familiar with RLU (Report Layout Utility), you could use it to create the DDS for the print file.
RLU is confusing to most AS/400 programmers and is not widely used.
When you have entered the DDS, use option 14 to compile it. Since the type is PRTF, the AS/400
compiler knows to execute command CRTPRTF.
Then, enter the RPG or RPG-ILE source code and compile. Notice that the RPG programs use the printer
file as the output file.
Call the program from the command line and a print file will be created. Use WRKSPLF to see it.
This is the RPG program for writing a Print Report using DDS. At the bottom, you will see a sample of
the report layout in the spool file.
Columns . . . : 1 71 Edit
USER000/QRPGSRC
SEU==>
TUTR007
FMT FX .....FFilenameIPEAF........L..I........Device+......KExit+
+Entry+A....U
0005.00 * is full.
0007.00
*----------------------------------------------------------------
0009.00 C WRITEHDG1
0010.00 C WRITEHDG2
106
0011.00 C WRITEHDG3
0014.00 C*
0018.00 C WRITEHDG1
0019.00 C WRITEHDG2
0020.00 C WRITEHDG3
0022.00 C ENDIF
0024.00 C WRITEDETAIL
0026.00 C ENDDO
0027.00 C*
0029.00 C RETRN
Once you have written the program, compile it using option 14. After
you have compiled the
program, you want to call it and see what the report looks like. To
call the program, simply
Next, go to the Work Spool File (WRKSPLF), find the program, and key
in a 5 to view the report.
107
This is what the report will look like:
108
001294 MC CULLUM .00 0
109
Writing an RPG Print Program using DDS
This is the RPG program for writing a Print Report using DDS. At the bottom, you will see a sample of
the report layout in the spool file.
Columns . . . : 6 76 Edit
USER000/QRPGLESRC
SEU==>
TUTR008
FMT FX FFilename++IPEASF.....L.....A.Device+.Keywords++++++++++++++++
+++++++++
0004.00 * That means that *IN10 will be *ON when the printed
page
0005.00 * is full.
0007.00
*----------------------------------------------------------------
0014.00 C*
110
0017.00 C If *In10 = *On
0022.00 C EndIf
0025.00 C EndDo
0026.00 C*
0028.00 C Return
Once you have written the program, compile it using option 14. After
you have compiled the
program, you want to call it and see what the report looks like. To
call the program, simply
Next, go to the Work Spool File (WRKSPLF), find the program, and key
in a 5 to view the report.
111
CUST # CUSTOMER NAME ORDERS ORDERS
112
11192002 CUSTOMER ORDER REPORT PAGE
0002
Finally, we get to the fun stuff! Hold on tight because things are about to get a lot more complicated.
Up to now, you have learned about the Control Language of the AS/400. You have also been introduced
to DDS. In fact, you have seen how to use DDS to define a Physical File, a Logical File and a Print File.
You have also seen some simple RPG programs.
113
Now, we will use DDS to create a Display File. Then, we will write a program to look up a Customer#
record and change it. In the next topic, we will add the ability to enter new records and to delete records.
Then, we will modify the program search by customer name.
Rather than talk you through all of the details, I am showing you the source code with lots of comments.
There are so many comments that it looks harder than it is. So, I have also provided source code without
the comments.
First, you must create a display file. This is often thought of as a screen map. It maps fields and characters
on the screen. Each record in the display file is a different screen.
You can copy the display file from the USER000 library or key it in yourself.
Before you get started, try using SDA to view the screens that I have created in USER000. First, start
SDA with the command STRSDA. Use selection 3 to TEST DISPLAY FILES. For the display file, use
TUTD010. For library, use USER000. For RECORD TO BE TESTED, put the cursor in the field and hit
F4. SDA will list all records defined. Select a record and hit ENTER a few times to see the screen.
To use SDA this way, the display file must already be created. That is, the source code must have been
entered and it must have been compiled successfully.
Now that you can see what you want to create, try keying in the source code.
To key it in, STRPDM. Use option 3. Fill in file QDDSSRC, in your library for *ALL member names.
Use F6 to add the member TUTD010. Make sure the TYPE is DSPF (for DISPLAY FILE).
When entering source code lines, try using F4 to prompt the fields. To turn off prompting, hit F12.
Click here to see the source code with all the comments. This is TUTD010 in QDDSSRC in USER000.
Click here to see the source code without all the comments. This is TUTD011 in QDDSSRC in
USER000.
When you are finished, compile the source code with option 14. This is running the command CRTDSPF
(Create Display File). When you are done, try using SDA again to test your display file.
The program will display the first screen, SCRN1. The user will enter an A to add, C to CHANGE or D to
DELETE. But for this portion, we will only program the CHANGE logic. In the next topice, we will add
the ADD and DELETE logic.
The program will first check to make sure that a C for CHANGE was entered. It then looks in the CUST
database file for the Customer #. If either of these conditions is not met, the program moves an error
message to the field DSPMSG and shows SCRN1 again.
If a Customer # is found, the program shows SCRN2. This screen shows the values in the record. If the
user changes any values and hits ENTER, the program will rewrite the changes to the database.
Click here to see the RPG III source code with all the comments. This is TUTR010 in QRPGSRC in
USER000.
114
TUTR010 in QRPGSRC in USER000
* (Keyed means the data can be directly read using the key field
FCUST UF E K DISK
FTUTD010 CF E WORKSTN
*----------------------------------------------------------------
I* as error messages
I ' or D'
I 'und'
I 'k'
*---------------------------------------------------------------
115
C *IN03 DOWEQ*OFF
C* EXFMT displays the SCRN1 and waits until the user presses
C EXFMTSCRN1
C* named MAIN.
C EXSR MAIN
C ENDIF
C ENDDO
C RETRN
C*----------------------------------------------------------------
C*
C MAIN BEGSR
C DSPCST CHAINCUST 95
C*
C*
116
C* Because this will eventually test for Add, Chg, Delete
C SELEC
C MOVELERR2 DSPMSG
C ELSE
C EXSR CHG
C ENDIF
C*
C OTHER
C MOVELERR1 DSPMSG
C ENDSL
C*
C ENDSR
C*----------------------------------------------------------------
C CHG BEGSR
C*
117
C* still blank after EDTVAL, then data is OK.
C*
C DSPMSG DOUNE*BLANKS
C EXFMTSCRN2
C EXSR EDTVAL
C UPDATCSREC
C LEAVE
C ENDIF
C ENDIF
C*
C ENDDO
C*
C ENDSR
C*----------------------------------------------------------------
C EDTVAL BEGSR
118
C CSNAME IFEQ *BLANKS
C MOVELERR3 DSPMSG
C ENDIF
C*
C MOVELERR4 DSPMSG
C ENDIF
C*
C MOVELERR5 DSPMSG
C ENDIF
C*
C MOVELERR6 DSPMSG
C ENDIF
C*
C MOVELERR7 DSPMSG
C ENDIF
C*
C ENDSR
Click here to see the RPG III source code without the comments. This is TUTR010NC.
119
FCUST UF E K DISK
FTUTD010 CF E WORKSTN
*----------------------------------------------------------------
I ' or D'
I 'und'
I 'k'
*----------------------------------------------------------------
C *IN03 DOWEQ*OFF
C EXFMTSCRN1
C EXFMTSCRN1
C*
C EXSR MAIN
C ENDIF
C*
C ENDDO
C*
C RETRN
C*----------------------------------------------------------------
120
C MAIN BEGSR
C*
C DSPCST CHAINCUST 95
C*
C*
C SELEC
C MOVELERR2 DSPMSG
C ELSE
C EXSR CHG
C ENDIF
C*
C OTHER
C MOVELERR1 DSPMSG
C ENDSL
C*
C ENDSR
C*----------------------------------------------------------------
C CHG BEGSR
C*
C DSPMSG DOUNE*BLANKS
C EXFMTSCRN2
121
C EXSR EDTVAL
C UPDATCSREC
C LEAVE
C ENDIF
C ENDIF
C*
C ENDDO
C*
C ENDSR
C*----------------------------------------------------------------
C EDTVAL BEGSR
C MOVELERR3 DSPMSG
C ENDIF
C*
C MOVELERR4 DSPMSG
C ENDIF
C*
C MOVELERR5 DSPMSG
C ENDIF
C*
122
C MOVELERR6 DSPMSG
C ENDIF
C*
C MOVELERR7 DSPMSG
C ENDIF
C*
C ENDSR
Click here to see the RPG IV source code with all the comments. This is TUTR011 in QRPGLESRC in
USER000.
* (Keyed means the data can be directly read using the key field)
FCUST UF E K DISK
FTUTD010 CF E WORKSTN
*----------------------------------------------------------------
D* as error messages.
123
D or D')
D und')
D k')
*----------------------------------------------------------------
C* EXFMT displays the SCRN1 and waits until the user presses
C ExFmt SCRN1
C If *in03 = *off
C* named MAIN.
C ExSr Main
C EndIf
C EndDo
C Return
124
C*----------------------------------------------------------------
C*
C Main BegSr
C*
C*
C Select
C If *in95 = *on
C Else
C ExSr ChangeRecord
C EndIf
C*
C Other
125
C* displayed in reverse image (see the DDS for DSPMSG).
C EndSl
C*
C EndSr
C*----------------------------------------------------------------
C ChangeRecord BegSr
C*
C*
C*
C *in12 = *on
C ExFmt SCRN2
C If *in12 = *off
C ExSr EditValues
C If DSPMSG = *blanks
126
C* database file, RPG synchronizes the values.
C Update CSREC
C Leave
C EndIf
C EndIf
C*
C EndDo
C*
C EndSr
C*----------------------------------------------------------------
C EditValues BegSr
C If CSNAME = *BLANKS
C EndIf
C*
C If CSADR1 = *BLANKS
C EndIf
C*
C If CSCTY = *BLANKS
C EndIf
C*
C If CSSTE = *BLANKS
127
C Eval DSPMSG = ERR6
C EndIf
C*
C EndIf
C*
C EndSr
Click here to see the RPG IV source code without the comments. This is TUTR011NC.
FCUST UF E K DISK
FTUTD010 CF E WORKSTN
*----------------------------------------------------------------
D or D')
D und')
D k')
*----------------------------------------------------------------
128
C DoW *in03 = *off
C ExFmt SCRN1
C If *in03 = *off
C ExSr Main
C EndIf
C EndDo
C Return
C*----------------------------------------------------------------
C Main BegSr
C Select
C If *in95 = *on
C Else
C ExSr ChangeRecord
C EndIf
129
C
C Other
C EndSl
C EndSr
C*----------------------------------------------------------------
C ChangeRecord BegSr
C *in12 = *on
C ExFmt SCRN2
C If *in12 = *off
C ExSr EditValues
C If DSPMSG = *blanks
C Update CSREC
C Leave
C EndIf
C EndIf
C EndDo
C EndSr
C*----------------------------------------------------------------
C EditValues BegSr
130
C* Make sure the values on the screen are OK
C If CSNAME = *BLANKS
C EndIf
C If CSADR1 = *BLANKS
C EndIf
C If CSCTY = *BLANKS
C EndIf
C If CSSTE = *BLANKS
C EndIf
C EndIf
C EndSr
131
The AS/400 has a built in debugging tool, though very powerful, it is not completely intuitive. Sometimes
you may find yourself in need of a procedure that can help you locate program errors that you cannot
visually find by looking at your program. RPG-ILE's program debugger can be very useful in these
situations. It allows you to trace a program as it is executing, stepping through a program a statement at a
time, or stopping at "breakpoints" that you designate so you can see the values of fields at that point of
execution. Follow the steps below to start the "debugging" mode for your programs.
First, you must compile the program in an appropriate mode. Do this from PDM by keying a 14 next the
program source and hitting F4.
Now, hit F10 to get more options. Key in *LIST as the value for "Debugging View".
132
Source file . . . . . . . . . . > QRPGLESRC Name, QRPGLESRC
Additional Parameters
Now ENTER to compile your program. If this program calls other programs, you may want to compile
them in debug mode also.
Now it gets a little tricky. Start the debugging mode with the "Start Debug" for the program you want to
debug. In this case, the program is LXR001 so the command to use is STRDBG LXR001. Key in
STRDBG LXR001 and hit F14:
*LIBL
133
Maximum trace statements . . . . 200 Number
If your program updates or writes data to a file in a library that is defined as *PROD, you must change the
"Update production files" option to *YES. Then, hit ENTER.
The debugger now displays the source code of the program. You MUST define a breakpoint where the
debugger will actually start debugging. The easiest thing to do is to page down to the first executable
statement of the program. Put the cursor on that line of source code and hit F6 to define that line as a
breakpoint.
Your session is now in "debug" mode. It will stay in "debug" mode until you end debug mode with the
command ENDDBG.
Finally, it is time to start running your program. Call the program from the command line. In this case:
CALL LXR001
The program will start running. The debug feature will display a listing of the program. The display is
showing that the breakpoint is about to execute. You can scroll up or down, position the cursor on any
field and hit F11. The debugger will display the value of the field.
Now hit F10 repeatedly to step through the source code. The line of code that is about to execute will be
highlighted.
Note that the debugger has the annoying habit of stepping through every field in each file that is read or
updated. To prevent this, add a "header" record as the first line of code in the program. "Header" records
are not required and not usually used. Use an "H" as the record type with a value of "option(*nodebugio).
That is, add this line to the beginning of your program:
H OPTION(*NODEBUGIO)
When you are finished debuggin, key ENDDBG to turn off the debugger.
To summarize:
3) Start the debugger with the command: STRDBG pgmname (Update Production files *YES)
134
4) Put cursor on the first line that you want to see and hit F6. Exit with F3.
6) Use F10 to step through the program. Use F11 to see values.
7) If you wish, set additional breakpoints and use F12 to run until the next breakpoint.
As before, try using F4 to prompt the fields. To turn off prompting, hit F12.
Also, as with all programs, compile the source code with option 14.
View your compile listing with the Work With Spool File command, WRKSPLF. Look for errors at the
bottom of the listing.
If you completed this topic, you are well on your way to becoming an RPG programmer.
Now that you have worked with RPG some, take a moment to review this maintenance program. It is
written in both RPG III and RPG IV.
Writing an Add/Change/Delete/Inquiry
maintenance program
This is the DDS for the Display Program named CUSTD01.
A REF(CUST)
A CA03(03 'F3=EXIT')
A R SCR1
A 1 2USER
A O 1 28'ADD/UPDATE/DELETE/INQUIRE'
A DSPATR(HI)
A 1 72DATE
A EDTCDE(Y)
A 2 2'CUSTR01'
A 2 72TIME
135
A O 6 8'(A)dd, (U)pdate, (D)elete'
A O 6 34'(I)nquire, (N)ext'
A ACTION 1 B 6 53DSPATR(HI)
A O 8 28'Cust#'
A ERRLIN 78 O 22 2
A 90 DSPATR(RI)
A O 23 14'F3=EXIT'
A*----------------------------------------------------------------
A R SCR2
A CA09(09 'F9=DELETE')
A 1 2USER
A 1 72DATE
A EDTCDE(Y)
A 2 2'CUSTR01'
A MODE 7 O 2 37
A 2 72TIME
A 3 28'Cust#'
A 8 28'Name'
A CUSNAM R B 8 42
A N80 DSPATR(HI)
A 80 DSPATR(PR)
A 9 28'Address 1'
A CUSAD1 R B 9 42
A N80 DSPATR(HI)
A 80 DSPATR(PR)
A 10 28'Address 2'
136
A CUSAD2 R B 10 42
A N80 DSPATR(HI)
A 80 DSPATR(PR)
A 11 28'City'
A CUSCTY R B 11 42
A N80 DSPATR(HI)
A 80 DSPATR(PR)
A 12 28'State'
A CUSSTA R B 12 42
A N80 DSPATR(HI)
A 80 DSPATR(PR)
A 13 28'Zip'
A CUSZIP R Y B 13 42
A N80 DSPATR(HI)
A 80 DSPATR(PR)
A ERRLIN 78 O 22 2
A 90 DSPATR(RI)
A O 23 14'F3=EXIT'
This is the RPG III source code for the Add/Change/Delete program named CUSTR01.
FCUSTD01 CF E WORKSTN
FCUST UF E K DISK A
*----------------------------------------------------------------
I 'LE'
I 'E'
137
I 'NO MORE RECORDS' C ERR3
I 'ANK'
I 'ANK'
I ' D OR I'
I 'SSFULLY'
I 'CESSFULLY'
I 'CESSFULLY'
*----------------------------------------------------------------
* (a key list is not needed since this file has only 1 key
C KEYLST KLIST
C KFLD CUS#
*----------------------------------------------------------------
*----------------------------------------------------------------
C *IN03 DOWEQ*OFF
C EXFMTSCR1
138
C CLEARERRLIN
C SELEC
C EXSR ADDREC
C EXSR DLTREC
C EXSR INQREC
C EXSR NXTREC
C EXSR UPDREC
C OTHER
C CLEARERRLIN
C MOVELERR9 ERRLIN
C ENDSL
C ENDIF
C ENDDO
C RETRN
*----------------------------------------------------------------
C ADDREC BEGSR
139
C MOVE ' ADD ' MODE
C KEYLST CHAINCUST 91
C CLEARERRLIN
C MOVELERR1 ERRLIN
C ELSE
C EXSR ADDSCR
C ENDIF
C ENDSR
*----------------------------------------------------------------
*----------------------------------------------------------------
C ADDSCR BEGSR
C *NOKEY CLEARCUSTR
C RECOK DOWEQ'N'
C *IN03 ANDEQ*OFF
C EXFMTSCR2
C EXSR EDTCUS
C WRITECUSTR
140
C CLEARERRLIN
C MOVELMSG1 ERRLIN
C ENDIF
C ELSE
C CLEARERRLIN
C MOVELMSG9 ERRLIN
C ENDIF
C ENDDO
C ENDSR
*----------------------------------------------------------------
C DLTREC BEGSR
C CLEARERRLIN
C MOVELMSG4 ERRLIN
C KEYLST CHAINCUST 91
C CLEARERRLIN
C MOVELERR2 ERRLIN
C ELSE
141
C EXFMTSCR2
C DELETCUSTR
C CLEARERRLIN
C MOVELMSG3 ERRLIN
C ELSE
C CLEARERRLIN
C MOVELMSG9 ERRLIN
C ENDIF
C ENDIF
C ENDSR
*----------------------------------------------------------------
C INQREC BEGSR
C KEYLST CHAINCUST 91
C CLEARERRLIN
C MOVELERR2 ERRLIN
C ELSE
142
C EXFMTSCR2
C ENDIF
C ENDSR
*----------------------------------------------------------------
C NXTREC BEGSR
C CLEARERRLIN
C KEYLST SETLLCUST 92 93
C MOVELERR3 ERRLIN
C ENDIF
C READ CUST 90
C MOVELERR3 ERRLIN
C ENDIF
C ENDIF
143
* to read past it
C READ CUST 90
C MOVELERR3 ERRLIN
C ENDIF
C ENDIF
C ENDIF
C EXFMTSCR2
C ENDIF
C ENDSR
*----------------------------------------------------------------
C UPDREC BEGSR
C KEYLST CHAINCUST 91
C CLEARERRLIN
144
C MOVELERR2 ERRLIN
C ELSE
C EXSR UPDSCR
C ENDIF
C ENDSR
*----------------------------------------------------------------
*----------------------------------------------------------------
C UPDSCR BEGSR
C RECOK DOWEQ'N'
C *IN03 ANDEQ*OFF
C EXFMTSCR2
C EXSR EDTCUS
C UPDATCUSTR
C CLEARERRLIN
C MOVELMSG2 ERRLIN
C ENDIF
C ELSE
C CLEARERRLIN
C MOVELMSG9 ERRLIN
C ENDIF
145
C ENDDO
C ENDSR
*----------------------------------------------------------------
C EDTCUS BEGSR
C CLEARERRLIN
C MOVELERR7 ERRLIN
C ENDIF
C CLEARERRLIN
C MOVELERR8 ERRLIN
C ENDIF
C CLEARERRLIN
C MOVELERR6 ERRLIN
146
C ENDIF
C ENDSR
*----------------------------------------------------------------
This is the RPG IV source code for the Add/Change/Delete program named CUSTR01.
FCUSTD01 CF E WORKSTN
FCUST UF A E K DISK
*---------------------------------------------------------------------
D RecOK S 1a
*---------------------------------------------------------------------
D LE')
D LE')
*---------------------------------------------------------------------
C KEYLST KLIST
147
C KFLD CSNBR
C*---------------------------------------------------------------------
C ExFmt SCR1
C If *in03 = *off
C Select
C ExSr AddRecord
C ExSr DltRecord
C ExSr InqRecord
C ExSr NextRecord
C ExSr UpdRecord
C Other
C EndSl
C EndIf
C EndDo
148
C Return
C*---------------------------------------------------------------------
C AddRecord BegSr
C* since we are in ADD mode, set the indicator off to allow field entry
C If *in91 = *off
C Else
C ExSr AddScreen
C EndIf
C EndSr
C*---------------------------------------------------------------------
C AddScreen BegSr
C *in03 = *off
C ExFmt SCR2
149
C If *in03 = *off
C ExSr EditRecord
C If recOK = 'y'
C Write CSREC
C EndIf
C Else
C EndIf
C EndDo
C EndSr
C*---------------------------------------------------------------------
C DltRecord BegSr
C* since we are in DLT mode, set the indicator on for no field entry
C If *in91 = *on
C Else
150
C* If customer is on file, show screen again and see if user hit F9
C* to confirm delete
C ExFmt SCR2
C If *in09 = *on
C Delete CSREC
C Else
C EndIf
C EndIf
C EndSr
C*---------------------------------------------------------------------
C InqRecord BegSr
C* since we are in DLT mode, set the indicator on for no field entry
C If *in91 = *on
C Else
C ExFmt SCR2
C EndIf
151
C Eval *in03 = *off
C EndSr
C*---------------------------------------------------------------------
C NextRecord BegSr
C If *in92 = *on
C EndIf
C If ERRLIN = *BLANKS
C Read CUST 90
C If *in92 = *on
C EndIf
C EndIf
C ERRLIN = *blanks
C Read CUST 90
152
C If *in90 = *on
C EndIf
C EndIf
C If ERRLIN = *blanks
C ExFmt SCR2
C EndIf
C EndSr
C*---------------------------------------------------------------------
C UpdRecord BegSr
C If *in91 = *on
C Else
C ExSr UpdScreen
C EndIf
C EndSr
C*---------------------------------------------------------------------
C UpdScreen BegSr
153
C DoW RecOK = 'n' and
C *in03 = *off
C ExFmt SCR2
C If *in03 = *off
C ExSr EditRecord
C If RecOK = 'y'
C Update CSREC
C EndIf
C Else
C EndIf
C EndDo
C EndSr
C*---------------------------------------------------------------------
C EditRecord BegSr
C If CSNAME = *blanks
C EndIf
C If CSSTE = *blanks
154
C EndIf
C If CSZIP = *zero
C EndIf
C EndSr
*----------------------------------------------------------------
Learning to use the tools on the AS/400 is one of the best ways to leverage your expertise.
Managers may be surprised to learn how easy it is to provide users with sample report layouts
that can be designed with an on-line tool. This on-line tool is RLU, Report Layout Utility.
Because many programmers find its interface to be confusing, not many have become proficient
RLU users. Read on and you will learn to create report layouts quickly and easily.
In the early days of RPG, reports were always printed using OUTPUT specifications. With the
advent of DDS, it became possible to make print files using DDS. This is similar to the way DDS
is used to make display files. The AS/400 provides SDA, Screen Design Aid, as an on-line tool
to design display files. Similarly, RLU, is an on-line tool to design reports.
1. Customer number
2. Customer name
3. Customer city
These fields are in the customer master file, CUST. We will write an RPG program, CUSR005 to
read the CUST file and write a print file, CUSW005.
Figure 1 shows the finished report that we will design with RLU. To start RLU, enter the
command STRRLU and hit F4. Enter QDDSSRC as the source file. Enter the library name and
CUSW005 as the source member. Since this is a narrow report, enter 65 for the page width. For
a standard width report, you would use 132 for the page width.
RLU now displays its DESIGN REPORT screen. RLU uses so many function keys that there is
a base set of function keys and an alternate set of keys. F22 is used to toggle between the base
155
keys and the alternate keys. Right now RLU shows the word, BASE in the upper left portion of
the screen to indicate that the base set of function keys is in use. F22 changes to the alternate
keys and the changes the display from BASE to ALT.
The simplest way to use RLU is to define each line as a separate record. To start working with a
line of the report, key DR for DEFINE RECORD in the command area on a blank line. When you
hit the enter key, RLU names the record with the default name RCD001. After defining a record,
enter VF for VIEW FIELDS in the command area of the same line. RLU then adds a line above
the record to show the field information.
The heading for the report will be CUSTOMER ANNUAL PURCHASES. On the line with
RCD001, key in the heading. Then, convert this string of characters to a constant. Do this by
placing the cursor on the first character of the heading and hitting F11 for DEFINE FIELD. Hit
F11 again and then ENTER to convert the string of characters to a constant. Finally, center the
heading by keying CF for CENTER FIELD on the command portion of the RCD001 line. When
you hit ENTER, the heading will center.
Now, create a second record. This will be used to print the customer fields. First insert a line to
work with. Key I for INSERT in the command area of the RCD001 line and hit ENTER. Then, as
before, key DR to define this as a record and then VF for VIEW FIELDS.
Next, use RLU to retrieve the field definitions from the master file. Hit F10 for DATABASE
FIELDS. Key 1 for the option to ADD FIELD. Leave the FIELD value blank. Enter the library
name and CUST for the File. Enter the record name of the CUST file. RLU will then retrieve all
the fields in the CUST file and list them. Select the fields for the report. Hit ENTER until you
return to the DESIGN REPORT screen.
The names of the fields you selected are at the bottom of the screen. Each field is sequentially
numbered. To insert the first field in the report, key 1 in the text area of the FLD record above
the detail record line. When you hit ENTER, RLU puts that field in the layout. RLU has re-
sequenced the remaining fields. Place the remaining fields in the report.
If you overlap fields, RLU can get confusing. The simplest way to recover is to delete the record
by keying a D in the command area of the record. Then start the record over. If you want to
remove a field, put the cursor on the field and hit F16. If you need to move a field, use F13 to
mark where the cursor is positioned and then F15 to move the field to a new location.
Now that you have placed the fields in a detail record, create headings for the detail line. Insert
a line above the detail line. As always, key DR for DEFINE RECORD and then VF to VIEW
FIELDS. Simply key the headings in the text area of the blank record that you inserted. Then
convert each field to a constant by putting the cursor on the first character of each literal and
hitting F11 twice.
To help visualize the report, create some sample data lines. Make 5 copies of the detail line by
keying SD5 on the command line of the detail line.
Now, add a line at the bottom of the report for the total annual purchases. Do this by keying I in
the command line of the last record on the screen. As always, key DR and then VF in the
command area of the record.
Key the word TOTAL in the text area of the record. Convert the word to a constant by placing
the cursor on the first letter of the word and hitting F11 twice. Now, add a new field for the
accumulated total. This can get confusing.
156
Place the cursor where you want the total amount to print. Hit F11 to DEFINE FIELD. On the
DEFINE FIELD INFORMATION screen, change the name of the field from the default FLD001
to TOTPUR. Change the length of data to 9. Hit PAGE DOWN. Change the data type to 2 for
ZONED and enter 2 for decimal positions. Then hit F10 to WORK WITH KEYWORDS. Select
EDTCDE by keying 2 next to it. Hit ENTER and key 1 for the EDTCDE value. Hit ENTER a few
times and you will see that you have added a field for the total purchases.
Finally, add the date and page counter to the first heading line. Do this by adding a field of the
appropriate length, converting it to a constant and then selecting the appropriate keyword. Also,
rename the records by using F18 for RECORD KEYWORDS and then F10 to rename the
record.
Exit RLU with F3. You can save the DDS, compile it and generate a prototype report all from the
exit screen.
157
00008 S 999999 XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX
99,999.99
00009 S 999999 XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX
99,999.99
00010 S 999999 XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX
99,999.99
FLD1 <...>
<..........>
TOTL TOTAL
9,999,999.99
FCUST IF E K DISK
FCUSW005 O E PRINTER OFLIND(*In99)
C Move *On *In99
C Read CUST
90
C *In90 DoWEq *Off
C *In99 IfEq *ON
C Write HDG1
C Write HDG2
C Move *Off *In99
C EndIf
C Add CUSAP TOTPUR
C Write DTL
C Read CUST
90
158
C EndDo
C Write TOTL
C Eval *INLR = *On
C Return
159