One of the greatest inventions of the 20th century is the sticky note. It allows you to jot something down, tear it off, and stick it on or near the object its referring to. In the AS/400 world, data areas are kind of like sticky notes. If you need a place to store a single value, such as a counter or account number, you can create a data area. And thats often what happens, resulting in applications ending up with a lot of data areas scattered around. Unfortunately, also like sticky notes, these data areas can be difficult to manage and organize.
The AS/400 operating system uses a different approach to organize values like this. These values are called system values, and the AS/400 organizes them more like a looseleaf binder with index tabs, providing commands such as Work with System Value (WRKSYSVAL), Change System Value (CHGSYSVAL), Display System Value (DSPSYSVAL), and Retrieve System Value (RTVSYSVAL) to manage them. This method works so well that Ive decided to emulate this process to create what I call user values. User values are like system values in that they are miscellaneous data that can be accessed globally through standard interfaces.
To manage user values, I have created utilities in the form of commands and APIs. You can find these utilities on the MC Web site at www.midrangecomputing.com/ mc/99/07. The commands are the preferred method for accessing user values from a command line or CL program. The APIs are the better choice for accessing user values from RPG programs. Figure 1 shows a list of the commands and APIs that access user values.
The first step is to create a user value infrastructure by calling the Create User Value (CRTUSRVAL) program. This program creates a physical file named USRVAL, which I prefer to place in library QUSRSYS, in which user values are stored. CRTUSRVAL also creates the necessary display files, programs, and commands.
To create a new user value, use the Add User Value (ADDUSRVAL) command. In the command parameters, specify the user value name (up to 10 characters), a description (the default is the name), the size of the user value, and the initial value. Maximum size for a user value is 64 bytes; I felt this was large enough to accommodate most user values. In
fact, most user values are probably much smaller, so I use any unused space in the field that holds the value to store additional text, which you can specify when creating the user value. If the user value already exists, you must specify REPLACE (*YES), or else ADDUSRVAL issues escape message CPF9898.
After you have created the user value, it can then be used, either interactively or by a program. To change the user value, use the Change User Value (CHGUSRVAL) command. Specify the user value name, the new value, and the number of seconds to wait if the user value is locked. (The following paragraph discusses locked user values.) If the user value doesnt exist, error CPF9897 is generated. If another job is using the user value and the wait time you specify expires, CPF9898 is generated. Otherwise, the user value is changed to the new value. The date and time of the last change are also updated, as well as the job name, user ID, and job number performing the update. This function is useful when debugging or tracking problems.
If you have a program that needs access to a user value, Retrieve User Value (RTVUSRVAL) is the command for you. Pass to it the user value name and a variable, and it returns the current value. Because your program may need to change the user value (incrementing a counter, for example) after it has been retrieved, RTVUSRVAL allows you to specify a number of seconds to lock the user value. When the user value is locked, no other job can retrieve or change it until the job that locked it either updates it or unlocks it by executing another RTVUSRVAL, this time specifying *UNLOCK. However, as soon as the number of seconds specified expires, the user value is fair game again. This prevents permanent locks from occurring when a job is caught in a loop or blows up.
To display the value and status of a user value, use the Display User Value (DSPUSRVAL) command. Figure 2 shows a sample display. From this screen, you can also change the user value by pressing F7.
Putting it all together is the Work with User Value (WRKUSRVAL) command, shown in Figure 3. This command lists all defined user values and allows you to display, change, or delete them. You can also add new user values by pressing F6.
Replace Those Data Areas!
Instead of cluttering up your libraries with myriad data areas, consider collecting all these miscellaneous bits and pieces of information into one file. User value utilities are a good way to organize your sticky notes on the AS/400. Put them to work today.
Command API Description
ADDUSRVAL UV0001 Add a new user value CHGUSRVAL UV0002 Change a user value DLTUSRVAL UV0002 Delete a user value DSPUSRVAL UV0003 Display the value of a user value RTVUSRVAL UV0002 Retrieve a user value into a CL variable WRKUSRVAL UV0005 Work with user values
Figure 1: Use these commands and programs to access user values.