Update database(Physical and Logical) Files from display screen in ILE RPG

Update PF and LF with User Input in RPGIV

Ok, So far we have learnt how to write user input to a database file and then how to read those records and display them on screen. Now, we will learn how to update the database file.

To update a database file we need to do the following.

  • Read the specific record from database (The record which is to be updated.)
  • Change the values of the file fields with new values.
  • Perform an update operation on the database file using the opcode UPDATE.

That's it!

If we see the "how to update database file" steps above, we know how to read specific record (Remember SetLl, Read?). We can also modify the values after read. The third step is a little unknown. The opcode UPDATE therefore is explained below.


ILE RPGIV OpCode UPDATE
The ocode UPDATE in ILE RPG updates the last read record with current values. This means that if you do not change values after read on the file, the previous values are updated.

A successful READ (Or Chain, READP, READPE, READE etc.) is mandatory before trying an UPDATE on a file. If you try to UPDATE a file without read, the program will crash with error something like ("Invalid operation sequence...") as the record which is to be updated has not been set. To avoid this error, many people prefer to do a READ on ad hoc basis(i.e whenever they need to update file, they follow the SETLL, READ, change values, UPDATE steps). You might follow this step.

To avoid unusual termination of program due to invalid operation you should use the E extender with it (Old RPG programmers used to place indicator at LOW position which is set on in case of update error).

Other important thing about UPDATE opcode is that, one Read Operation can follow only One UPDATE. It should not be like you read any record once and keep on updating it whenever you feel like updating :).

This is because when you read a record from a file declared in update mode, that record is locked to the program. This lock is necessary to maintain data integrity. When you update the program, the record is updated and unlocked. Once a record is unlocked, you can not update it. Lock is required to update a file.

To intentionally unlock a record, use the Opcode Unlock and specify the file name (not the record format name) in the factor 2.

For now this enough for the opcode UPDATE. Lets this opcode in action.


Example to update Physical and Logical Files
 **                                                                    
 ** Declare the logfile in UPDATE mode                                 
 **                                                                    
FFilename++IPEASF.....L.....A.Device+.Keywords+++++++++++++++++++++++++
FLOGFILE   UF   E             Disk                                     
FLOGDSPF   CF   E             WorkStn                                  
 **                                                                    
 ** Read the file                                                      
 **                                                                    
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq
C     1             SetLl     LOGFILE                                  
C                   Read      LOGFILE                                90
 **                                                                    
C                   DoW       Not (*INKA Or *In90)                     
 **                                                                    
C                   Eval      $Num1 = num1                             
C                   Eval      $Num2 = num2                             
C                   Eval      $Sum = $Num1 + $Num2                     
 **                                                                    
 ** Display screen. Value is to be changed on screen                   
 **                                                                    
C                   ExFmt     LOG02                                    
 **                                                                    
 ** Ensure exit key has not been pressed                               
 **                                                                    
C                   If        *InKa = *Off                             
 **                                                                    
 ** Change the file fields values                                      
 **                                                                    
C                   Eval      Num1 = $num1                             
C                   Eval      Num2 = $num2                             
 **                                                                    
 ** Perform actual update on the file                                  
 **                                                                    
C                   Update    LOGPF01                                  
C                   EndIf                                              
 **                                                                    
 ** Read next record                                                   
 **                                                                    
C                   Read      LOGFILE                                90
 **                                                                    
C                   EndDo                                              
 **                                                                    
C                   Eval      *InLr = *On                              
C                   Return                                             


//The display and physical file source
** The physical file
A               R LOGPF01                  
A                 USER          10A        
A                 NUM1          15P 0      
A                 NUM2          15P 0      
A               K USER                     


// Display File
A                                      DSPSIZ(24 80 *DS3)              
A          R LOG02                                                     
 ** FUNCTION KEY DEFINED                                               
A                                      CF01                            
 ** SCREEN FIELDS AND LABELS                                           
A          R LOG01                                               
A                                      CF01                      
A                                 11 19'Please Enter Your Name :'
A            $USER         10   B 11 45                          
A                                 23  3'F1=Exit'                 
A                                      COLOR(BLU)     
 ** 
A                                  5  2'Enter two Numbers and Press Ent
A                                      to get their sum.'              
A                                  7  2'Enter First Number :'          
A            $NUM1         10  0B  7 23                                
A                                  8  2'Enter Second Number:'          
A            $NUM2         10  0B  8 23                                
A                                 10 23'-----------'                   
A                                 11 17'Sum ='                         
A            $SUM          11  0O 11 23COLOR(WHT)   
A                                 23  2'F1=Exit'                 
A                                      COLOR(BLU)                   
A                                  2  2'Sum Of Two Numbers'   
A                                      COLOR(WHT)        
A                                      DSPATR(UL)