mibuso.com

Microsoft Business Solutions online community
It is currently Sat May 25, 2013 2:05 pm

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Move a record from one table to another
PostPosted: Fri Feb 15, 2013 11:41 pm 
Offline

Joined: Fri Feb 15, 2013 12:10 am
Posts: 1
Country: United States (us)
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.


Top
 Profile E-mail  
 
 Post subject: Re: Move a record from one table to another
PostPosted: Sat Feb 16, 2013 1:01 am 
Offline
Site Admin
MVP Microsoft Dynamics NAV
NAV TechDays 2013 attendee

Joined: Sun Nov 07, 1999 8:01 am
Posts: 3294
Location: Wilrijk
Country: Belgium (be)
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
Code: Select all
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.
Search is your friend || Mark your Topic as SOLVED (= green checkmark) when your question is answered || Read the Forum Rules before making a posting

»»» Mark your calendar: NAV TechDays 2013 - 7 & 8 November 2013 ««« Visit the conference website: www.navtechdays.com


Top
 Profile  
 
 Post subject: Re: Move a record from one table to another
PostPosted: Sun Feb 17, 2013 12:15 am 
Offline

Joined: Fri Nov 09, 2007 1:20 pm
Posts: 254
Location: Germany
Country: Germany (de)
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:

Code: Select all
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()


Top
 Profile  
 
 Post subject: Re: Move a record from one table to another
PostPosted: Sun Feb 17, 2013 12:22 am 
Offline

Joined: Fri Nov 09, 2007 1:20 pm
Posts: 254
Location: Germany
Country: Germany (de)
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: BernardJ, Google [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum


Search for:
Jump to: