0

How to Duplicate a table also with a subtable

Just quickly created Table 1 and Copy Table 1 for example, I'm wondering duplicate  Table 1(Table 1-1) to Copy Table 1(Copy Table 1-1), but only able to duplicate the main table's field, subtable got nothing to create. Does anyone know where is the problem?

let i := null;
let xDate := Date;
let xInvoiceNumber := 'Invoice Number';
let xType := Type;
let i := (create 'Copy Table');
i.(Date := xDate);
i.('Invoice Number' := xInvoiceNumber);
i.(Type := xType);
for j in select 'Table 1-1' do
    let k := (create 'Copy Table 1-1');
    k.(Items := j.Items);
    k.(Description := j.Description);
    k.(Qty := j.Qty);
    k.('Unit Price' := j.'Unit Price');
    k.(Cost := j.Cost)
end

Many thanks

Roger

2 replies

null
    • Fred
    • 2 yrs ago
    • Reported - view

    so if you put the for loop in a button by itself and run it no new records are recreated? You can also modify it so you don't have to repeat the variable:

    k.(Items := j.Items;
       Description := j.Description;
       Qty := j.Qty)
    end
    
    • Fred
    • 2 yrs ago
    • Reported - view

    Re-reading your topic again, you talk about subtables.

    Here is an example of code in a button that will copy the current record you are on. Here is my structure. Table2 (parent) > child (child of Table2)  and Table1 (parent) > Table1Child (child of Table1). The reference field in Table1Child that links to Table1 is called Table1 (yeah it can get confusing).

    In a button in Table2 I put:

    let t := this;
    let newPrec := (create Table1);
    newPrec.(Firm := t.Name);
    for loop1 in t.child do
        let newCrec := (create Table1Child);
        newCrec.(
            Table1 := newPrec;
            Text := loop1.Text
        )
    end
    

    Line 1 uses the this command to gather the data from the current record.

    Line 2 creates a new record in Table 1, stores the info in a variable called newPrec (or new Parent Record) this will come in handy later.

    Line 3, copies the data from Name (Table2) to Firm (Table1).

    Line 4 - 10 is a for loop command that will take all related records in the Child table of the current record you are on in Table 2 and copy them to Table1Child table and link them to the newly created record in Table1.

    Line 4 sets up the loop and since Child is a reference field we can use it to quickly find all related records (no need for a select). Then we just add the variable t in front so Ninox knows to find all Child records of the current record.

    Line 5 creates a new record in the table Table1Child

    Line 7 sets the link of the reference Table1 to the new record created above using the newPrec.

    Line 8 is an example of coping data from one field to another.

Content aside

  • Status Answered
  • 2 yrs agoLast active
  • 2Replies
  • 170Views
  • 2 Following