It seems like there are three different ways to solve any problem on the AS/400. Even experienced IS professionals sometimes loose sight of alternate approaches to application design. In this training track series I present basic AS/400 skills to help you keep those solutions in sight. I'll show you how to use data areas to store control values for a fictitious invoicing system.
Your invoicing system needs to keep track of the last invoice number used. You might also want to store the sales tax percentage in a way that makes it easy to change. Finally, suppose you have a standard handling charge that is added to each invoice.
One possible approach to storing these control values is to create a database file with one record. That record would have a field for last invoice number, another field for sales tax percentage and another for handling charge. The invoicing program would chain to this record and use those values. You could then write a maintenance program so the administrator of the system could change the values.
The AS/400 offers another approach. You could create a data area to store these values. Think of a data area as a chunk of memory that is similar to a file with one record. You can change the value of any character in the data area and you can retrieve the values from any program: CL, RPG and COBOL.
To see the different ways of using data areas, I'll show you how to:
- 1) create the data area and assign it values using commands
- 2) retrieve, use and update the data area using an RPG program
- 3) use a CL program to give the system administrator an easy way to change the values
CRTDTAARA DTAARA(INVFILES/INVA100) TYPE(*CHAR) LEN(100)
You now have 100 characters of storage to work with. Suppose your invoice numbers are 7 positions long and your last invoice number was 100324. You want to change the value of the first 7 characters of the data area to 0100324. You do this with the "Change Data Area" command:
CHGDTAARA DTAARA(INVFILES/INVA100 (1 7)) VALUE('0100324')
Then use these commands to store your sales tax of 7.125% in positions 8 through 12 and your handling charge of $2.00 in position 13 through 17:
CHGDTAARA DTAARA(INVFILES/INVA100 (8 5)) VALUE('07125') CHGDTAARA DTAARA(INVFILES/INVA100 (13 5)) VALUE('00200')
Now use this "Display Data Area" command to display your data area:
DSPDTAARA DTAARA(INVFILES/INVA100)
The AS/400 will show that you have successfully stored your control values in the data area. You have probably noticed the free format nature of this data area. There are no database fields or special attributes for the data area. It will be up to you to keep track of the location of each value in the data area.
I won't look at a complete invoicing program here. The important thing for now is that your RPG program will need to retrieve the data area at the beginning of the program. Also, when the program has finished creating the invoices, it needs to update the value of the last invoice number.
Figure 1 shows the RPG code to retrieve and update the data area. It retrieves the data area with the "IN" instruction. This instruction reads the 100 characters in the data area named "INVA100" into the data structure named "ControlValues". This data structure breaks up the data area into the three fields that you need: CtlLastInv#, CtlSalesTax and CtlHandling.
When the main logic of the program creates the invoices, it will increment the invoice number. Before terminating, the program uses the "OUT" instruction to update the data area. After the program runs, you could use the "Display Data Area" command to see that the value of the last invoice number in the data area has changed.
You could document how the system administrator could use the "Change Data Area" command to modify the sales tax and handling charge. It is more user friendly to write a program to format the fields on a display file. You could write an RPG program that retrieves the data area with an "IN" instruction, displays it using a display file and updates it with an "OUT" instruction. Since I want you to see different ways of using data areas, I'll use a CL program to display and change the values in the data area.
CL programs can declare one file. If this file is a display file, the CL can display and read a screen. If you code and compile a simple display file, a CL program can use it to provide an easy way to work with the values in the data area.
First, create a display file named "INVD100" with three fields:
- DLSTIN for last invoice #
- DSLSTX for sales tax
- DHNDCH for the handling charge.
I can't leave the topic of data areas without mentioning the "Local Data Area". Every job running on the AS/400 has a system defined data area of 1,024 characters. This means that any interactive job has this area available at all times. Though this isn't considered a modern programming style, many programmers use the "Local Data Area" as a way to pass parameters between programs. You can use the "Display Data Area" and "Change Data Area" commands to work with the "Local Data Area". Instead of specifying a data area name, use "*LDA" to indicate that you are referring to the system defined "Local Data Area". Realize that each job has its own "Local Data Area" which is cleared when you sign off.
Figure 1 - This is all you need to read and update the data area from an RPG program.
0001.00 * This defines the data structure that defines the fields
0002.00 * of the data area named INVA100
0003.00 D ControlValues DS DtaAra(INVA100)
0004.00 D CtlLastInv# 1 7 0
0005.00 D CtlSalesTax 8 12 3
0006.00 D CtlHandling 13 17 2
0007.00 D CtlFiller 18 100
0008.00 * First time routine to retrieve the data area
0009.00 C *Lock IN ControlValues
0010.00 *
0011.00 * The main logic of the program goes here
0012.00 *
0013.00 * Before ending program, update the data area since CtlLastInv#
0014.00 * has changed
0015.00 C OUT ControlValues
0016.00 C Eval *InLR = *On
0017.00 C Return
Figure 2 - This is the CL program to easily modify the values of the data area.
0001.00 PGM
0002.00 /* This DCLF (declare file) names the display file that will be used */
0003.00 DCLF FILE(INVD100)
0004.00 /* These Retrieve Data Area commands read the data area and put the */
0005.00 /* the values in the fields in the Display File */
0006.00 RTVDTAARA DTAARA(INVA100 (1 7)) RTNVAR(&DLSTIN)
0007.00 RTVDTAARA DTAARA(INVA100 (8 5)) RTNVAR(&DSLSTX)
0008.00 RTVDTAARA DTAARA(INVA100 (13 5)) RTNVAR(&DHNDCH)
0009.00 /* Send Receive File shows the display & then reads what is entered */
0010.00 SNDRCVF RCDFMT(SCR1)
0011.00 /* These Change Data Area commands store what was entered back in the*/
0012.00 /* Data Area */
0013.00 CHGDTAARA DTAARA(INVA100 (1 7)) VALUE(&DLSTIN)
0014.00 CHGDTAARA DTAARA(INVA100 (8 5)) VALUE(&DSLSTX)
0015.00 CHGDTAARA DTAARA(INVA100 (13 5)) VALUE(&DHNDCH)
0016.00
0017.00 ENDPGM
Tidak ada komentar:
Posting Komentar