0

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?

1
  • you've asked a lot of very basic question. Consider finding a training class or at the very least take a look at the RPG Tutorial. Commented Jun 15, 2021 at 13:58

2 Answers 2

2

use the CHGPF command to change the CUREXG file so it has 2 key fields: EXGDAT and EXGCOD

     A          R CURREC              
     A            EXGDAT          L   
     A            EXGCOD         3A   
     A            EXGRAT         5P 2 
     A          K EXGDAT              
     A          K EXGCOD              
CHGPF FILE(CUREXG) SRCFILE(QDDSSRC) SRCMBR(CUREXG) 

then, in the RPG, chain to the CUREXG file with the EXGDAT key and the 2nd EXGCOD key:

 /free
      chain       ( exgdat: 'USD' ) currec ;
      if          %found ;
      exgrat      = usd ;
      update      currec ;
      endif ;
 /end-free
Sign up to request clarification or add additional context in comments.

7 Comments

I tried this coding, but somehow got infine loop, but after i put CHAIN (N) then, got some error, which update or delete without prior input operation. What should i do?
don't use CHAIN(N). Use CHAIN to read and lock the record for update. CHAIN(N) will read the record but not lock it.
But when i didnt put chain(n), it will run infinite loop instead
And after awhile, got new error which is 'I/O error CPF5032 was detected'
use the DSPMSGD command to see the text of the CPF5032 error. It says "record already locked". When you read and lock a record you have to then either release the lock or do an update. If you read and lock the same record again, you get that "record already locked" error. Note that whether you READ or CHAIN a file that is opened for update, the record will be locked. Use the (N) option whenever you READ or CHAIN a record you do not intend to update.
|
1

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;
**---------------------------------

15 Comments

Does this need to create logical file?
I also tried your coding, so far, after I compile, and try to update the record, it just update the next value. what I mean is, I try to edit value for USD on 31 May, but when I press enter and refresh the screen, weirdly the USD value for 1 June is the one that change. What should i do then?
What is the date field on the DSPF display screen for updating ? Sounds like you might not have the correct date field? is it Date2? In debug AFTER exfmt what is the value of date2? is it 31 May?
Okay I think I figured out your issue If you change the date, we need to detect that a change occurred and redisplay the screen (because that date could have different values) I updated the code by adding a loop that checks for a date change See if that helps!
Update : Now I move like define exgcod after dow for date, now it works and updated, but so far it only can update for USD value only. The rest of the value still remains unchanged. Do you think i need to do another loop to define after chain so that it can move each of the value?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.