Using a button to duplicate a record and then display that record

I am trying to add a button to my master/detail form that will duplicate the current record and it’s detail records and then edit that record. This is to be used as the starting point for a new record. To prevent problems with duplicate ID numbers and spurious entries, I think it would be best to redirect back to the same form but in Add mode and then prefill the form fields with the data from the copied record. How would I go about doing this?

Any help would be greatly appreciated.
Thanks,
Chris

[QUOTE=cnbrammer;27200]I am trying to add a button to my master/detail form that will duplicate the current record and it’s detail records and then edit that record. This is to be used as the starting point for a new record. To prevent problems with duplicate ID numbers and spurious entries, I think it would be best to redirect back to the same form but in Add mode and then prefill the form fields with the data from the copied record. How would I go about doing this?

Any help would be greatly appreciated.
Thanks,
Chris[/QUOTE]

More easy.

Create a PHP button and do an INSERT SQL sentence against the table with the values of the fields. Then do a SELECT over detail with old id, and Insert again with results using newID for master field.

cnbrammer - I was wondering if you were able to figure this out in scriptcase? I’m being asked to create the same situation. Any tips you could provide would be great. Although it’s a good reference, I’m not quite understanding Giu’s explanation. Thanks!

With sql you can replicate the record. With exit(#) where # is a number, see helpfile, you can restart the same module. If you add a global to your application for the where clause in the sql section:

where mykey=[glob_mykey] you can preselect the new inserted record if you have saved the last insert key value. Initially you have to set this global variable to ‘0 or 1=1’ in this case to get a full selection.

I finally got this written and working except for reloading the form so that it shows the newly created copy. I have tried sc_exit, sc_redir, and tried redirecting to a blank app and back to the form, with no luck. To complicate things, I have no WHERE clause in the form’s SQL because I am using Application Link from a grid application to the form that has this copy button. How do I get the form to redirect back to the newly created copy?

//Make a temporary table and copy the current order fields into it.
$sqls = "CREATE TEMPORARY TABLE OrderTemp ENGINE=InnoDB " .
"SELECT * FROM Orders " .
"WHERE OrderID = " . {OrderID};
sc_exec_sql($sqls);

//Set the OrderID of the temporary record to NULL to avoid key field conflict.
$sqls = “UPDATE OrderTemp SET OrderID = NULL”;
sc_exec_sql($sqls);

sc_exec_sql(“start transaction;”);

//Add the temporary order record to the orders database.
$sqls = "INSERT INTO Orders " .
"SELECT * FROM OrderTemp ";
sc_exec_sql($sqls);

//Save the new OrderID to a variable
$NewOrderID = mysql_insert_id();

sc_exec_sql(“DROP TABLE OrderTemp”);//Remove temporary table.

//Make a temporary table and store the items of the copied order.
$sqls = "CREATE TEMPORARY TABLE ItemTemp ENGINE=InnoDB " .
"SELECT * FROM Items " .
"WHERE OrderID = " . {OrderID};
sc_exec_sql($sqls);

//Set OrderID of the temporary item table records to the new OrderID.
$sqls = "UPDATE ItemTemp SET OrderID = " . $NewOrderID ;
sc_exec_sql($sqls);

//Set the ItemID of the temporary record to NULL to avoid key field conflict.
$sqls = “UPDATE ItemTemp SET ItemID = NULL”;
sc_exec_sql($sqls);

//Add the temporary item records to the items database.
$sqls = "INSERT INTO Items " .
"SELECT * FROM ItemTemp ";
sc_exec_sql($sqls);

sc_exec_sql(“DROP TABLE ItemTemp”);//Remove temporary table.

sc_exec_sql(“commit;”);

//Redirect to the form for the new order
sc_redir(“B1-OrderForm”);

I figured out how to get around it, I had to manually define the link between the grid and form. So I added a new field to the grid and set it to HTML image, chose an edit icon, and added a link to the form there, passing the same variable I used in the redirect on the copy function.

I know this is an old post but I found it whilst working out how to do this.

I’ve now noticed that there is a ‘Copy’ button in the toolbar - not sure how long that has been there for. This combined with the sc_btn_copy macro seems to cover most circumstances.

I was gnashing my teeth for not noticing it before!