Move a record from one table to another

BeekerBeeker Member Posts: 2
Hello,

I've worked with Nav 5.1 on SQL for a few years but now I'm trying to learn some simple programming. I've searched the forum on this topic but I can't find a detailed enough example that I can get to work. This is just for me to learn, it has no valid application at this point.

I have two tables and a form. I use the form to add records to the StudentApplicant table. If the student gets accepted I want to move the record out of the StudentApplicant table an into the EnrolledStudent table. The tables are layed out identically with a No., Name, and Age field. I have a command button on the StudentApplicant form.

My variables are
EnrolledStudent as type Record and sub type EnrolledStudent table
StudentApplicant as type Record and sub type StudentApplicant

My code i've tried and is pieced together from this forum is

EnrolledStudent.SETRANGE("No.", StudentApplicant."No.");
IF EnrolledStudent.FIND('-') THEN BEGIN
EnrolledStudent.Name := StudentApplicant.Name;
EnrolledStudent.Age := StudentApplicant.Age;
EnrolledStudent.MODIFY;
END ELSE BEGIN
Clear(EnrolledStudent);
EnrolledStudent."No." := StudentApplicant."No.";
EnrolledStudent.Name := StudentApplicant.Name;
EnrolledStudent.Age := StudentApplicant.Age;
EnrolledStudent.INSERT(TRUE);
END;

I created a processing only report and put this code in the OnPreDataItem section and run it from the command button but all that happens is a blank row is created in the EnrolledStudent table.

Can anyone point out what I'm doing wrong? It seems easy enough but I'm getting really frustrated.

Eventually, I'd like to try this on a table that uses number series but this is just using the No. as type code. I also don't think using the TRANSFERFIELDS function is a good idea because the two tables may not be identical in the near future.

Comments

  • Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    There is no code that reads a record from the StudentApplicant variable. This explains the empty record which is created, as no values are retrieved for No., Age, Name.

    You'll need code like
    StudentApplicant.SETRANGE(<your filters>);
    IF StudentApplicant.FINDSET THEN
      REPEAT
         <add code to insert record in other table>
      UNTIL StudentApplicant.NEXT = 0;
    
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • vaprogvaprog Member Posts: 1,141
    I agree with Luc concerning the reason, why a blank record is created. However, since you are in a ProcessingOnly report, you most likely just need to move your code from the OnPreDataitem trigger to the OnAfterGetRecord trigger, assuming the dataitem is StudentApplicant.

    You can liken a dataitem in a report with the following structure of code:
    Dataitem.OnPreDataitem();
    IF Dataitem.FINDSET THEN
      REPEAT
        Dataitem.OnAfterGetReocrd();
        IF NOT SkipCalled THEN BEGIN
          Dataitem.RunSections();
          Dataitem.RunSubDataitems();
        END;
      UNTIL Dataitem.NEXT = 0;
    Dataitem.OnPostDataitem()
    
  • vaprogvaprog Member Posts: 1,141
    On an additional note:
    For an application like this, you should design the two tables with matching field numbers for matching fields. Then you can use the TRANSFERFIELDS function instead of a long row of field assignments.
Sign In or Register to comment.