Move a record from one table to another

Beeker
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.
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.
0
Comments
-
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 likeStudentApplicant.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)0 -
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()
0 -
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.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions