Search code examples
updatesrpgle

How to UPDATE DIFFERENT RECORD from subfile to physical file in RPGLE?


How to Update value of different record from SUBFILE into Physical file?

The screen are as shown in picture below:

update

The value I want to update is for USD from 4.12 to 4.13.

This is my physical file :

pf

After I press enter, the value will just updated on the screen only, but when I runqry to check at physical file, there are no any changes happen. What should I do? Please help me. Thanks in advance :)

My concern : Now, it updated, but it just update the next value or the latest value for latest date. what I mean is, I try to edit value for USD on 31 May, but when I press enter and refresh the screen, I don't know why the USD value for 1 June is the one that change. And, I also try to update the other value at the same time, but only USD is the one that updated. What should I do then?


Solution

  • There is only one update for multiple records Try something like this

    UPDSR    Begsr
             MoveL(P)  'UPDATE'    Mode
    *******Exgdat   Chain     Curexg            9091
    *******         If        *In91 = *On
    *******         Leavesr
    *******         Endif
    *******         If        *In90 = *Off
    *******         Movel(p) Date     Date2
    *******         Movel(p) USD      USD2        
    *******         Movel(p) GBP      GBP2
    *******         Movel(p) EUR      EUR2
    *******         Movel(p) AUD      AUD2
    *******         Movel(p) SGD      SGD2
      // make a generic 10 alpha field to control your loop
               movel    *blank       @@Change    10A
       *like   Define     Date2      @@DispDate
     @@change = 'First';
     dow @@change <> *blank; //loop to check for date change
    
       clear @@change;
       @@DispDate = Date2;    // what is the date used to display
    
       @@gcod = 'USD'  
       exsr $ForDisplay;
       USD2 = @@grat;   
    
       @@gcod = 'GBP'  
       exsr $ForDisplay;
       GBP2 = @@grat;
    
       @@gcod = 'EUR'  
       exsr $ForDisplay;
       EUR2 = @@grat;
    
       @@gcod = 'AUD'  
       exsr $ForDisplay;
       AUD2 = @@grat;
    
       @@gcod = 'SGD'  
       exsr $ForDisplay;
       SGD2 = @@grat;
          
             Seton                        02
    N12      Exfmt     Screen
       if Date2 <> @@DispDate;  //the user changed the date!
         @@change = 'Change';
       endif;
     enddo;
    
    ******* User's answers
             Movel(p) Date2    Date
             Movel(p) USD2     USD 
       @@gcod = 'USD';
       @@grat = usd;
       exsr $UpdateRec;
                     
             Movel(p) GBP2     GBP
       @@gcod = 'GBP';
       @@grat = gbp;
       exsr $UpdateRec;
    
             Movel(p) EUR2     EUR
       @@gcod = 'EUR';
       @@grat = eur;
       exsr $UpdateRec;
    
             Movel(p) AUD2     AUD
       @@gcod = 'AUD';
       @@grat = aud;
       exsr $UpdateRec;
    
             Movel(p) SGD2     SGD
       @@gcod = 'SGD';
       @@grat = sgd;
       exsr $UpdateRec;
    
    
    *******           select
    *******   exgcod  wheneq 'USD'
    *******            move   usd   exgrat
    *******   exgcod  wheneq 'GBP'
    *******            move   gbp   exgrat
    *******   exgcod  wheneq 'EUR'
    *******            move   eur   exgrat     
    *******   exgcod  wheneq 'AUD'
    *******            move   aud   exgrat
    *******   exgcod  wheneq 'SGD'
    *******            move   sgd   exgrat
    *******            endsl
    
    *******N12        Update   Currec
               eval     MSG ='Record updated'
    *******           Endif
               Setoff                91
    
               Endsr
    **---------------------------------
      Begsr $ForDisplay;
    *** Using CHAIN(N) to avoid locking the file, this is to load the screen
    ** Assumption that the fspec for Curexg has a key list of exgdat, exgcod
    ** if it does not exist, you will need to create a logical that has one 
       *like   Define     exgcod   @@gcod
       *like   Define     exgrat   @@grat
      
        clear @@grat;
        chain(n) ( Date : @@gcod ) Curexg;
        if %found(Curexg);
          @@grat = exgrat;
        endif;
      Endsr;
    **---------------------------------
      Begsr $UpdatRec;
        if *in12 = *off; //allowed to update?
           chain ( Date : @@gcod ) Curexg;
           exgrat = @@grat;
           if %found(Curexg); //exists, update it
             update Currec;
           else;  //record doesn't exist yet, possibly new currency, new rec
             Exgdat = Date;
             Exgcod = @@gcod;
             write Currec;
           endif;
         endif;
      Endsr;
    **---------------------------------