[SOLVED] Copy a record ? lazarus / 16-Databases ---------------------------------------------------------------------- TheNick · 2021-05-25 13:43 Hi all, I'm trying to make a copy of an existing record into the same dataset with some entries changed. 1) I copy the contents to a XML-file with the SaveToFile-procedure 2) I append a a new record to the dataset 3) I now populate the dataset with the old data by using the LoadFromFile-procedure 4) I post the new record and set the mode to edit 5) I change some data No matter what I do, I end up with empty data. The code looks something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i DataModuleR ules.SQLSelectRulesList.SaveToFile('SQLSelectRulesList.xml', dfXML);  //This generates a nice XML- file DataModuleRules.SQLSelectRulesList.Append; if FileExists('SQLSelectRulesList.xml') Then       DataModuleRules.SQLSel ectRulesList.LoadFromFile('SQLSelectRulesList.xml'); //The file are being read but nothing happens... DataModuleRules.SQLSelectRulesList.P ost;DataModuleRules.SQLSelectRulesList.Edit;DataModuleRules.SQLSelectR ulesList.FieldByName('name').AsString:='Copy from - '+ DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString;   Anyone got an idea what is going wrong? //Cheers Niclas ---------------------------------------------------------------------- Zvoni · 2021-05-25 14:35 Is it just me, or is there an "ApplyUpdate" missing? EDIT: And shouldn't the LoadFromFile also have a Format specified? https://www.freepascal.org/docs- html/fcl/bufdataset/tcustombufdataset.loadfromfile.html EDIT2: Why don't you use "CopyFromDataset"? https://www.freepascal.org/docs- html/fcl/bufdataset/tcustombufdataset.copyfromdataset.html EDIT3: another one: Why not use "INSERT INTO SELECT"? https://www.w3schools.com/sql/sql_insert_into_select.asp ---------------------------------------------------------------------- egsuh · 2021-05-26 07:15 How to copy is your choice depending on your application's conditions, but I would suggest to use SQL statement itself to copy records.  Following is an example from Firebird documentation. Code: SQL  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i INSERT INTO Members (NUMBER, name)  SELECT NUMBER, name FROM NewMembers WHERE Accepted = 1    UNION  SELECT NUMBER, name FROM SuspendedMembers WHERE Vindicated = 1 And then you may change the data of copied records. Of course this is an option. ---------------------------------------------------------------------- TheNick · 2021-05-26 09:01 Thank you both for your examples and suggestions. From my pov a safe way of doing this (without defining to much stuff around) is by doing a classic record: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  with (rulerecord) do         begin              name:='Copy of - ' + DataModuleRules.SQLSelectRulesList.FieldByName('name').AsString;            FAIClass:= DataModuleRules.SQLSelectRulesList.FieldByName('class').AsString;            typeofcomp:=DataModuleRules.SQLSelectRulesList.FieldByName('ty pe').AsString;              created_at:=TimeStamp;              DataModuleRules.SQLSelectRulesList.Append;              DataModuleRule s.SQLSelectRulesList.FieldByName('name').AsString:=name;              DataModuleRules.SQLSelectRulesList.FieldByName('created_at').AsLargeIn t:=created_at;              DataModuleRules.SQLSelectRulesList.FieldBy Name('class').AsString:=FAIclass;              DataModuleRules.SQLSele ctRulesList.FieldByName('type').AsString:=typeofcomp;         end;   Can't say that I like it but it works. I was having higher hopes for the 'CopyFromDataSet' but that costed more of effort then to create an record-type (I have probably not figured out yet how to use in a correct way!). Cheers Niclas ---------------------------------------------------------------------- Zvoni · 2021-05-26 09:25 If i read it right, your SQLSelectRulesList is already of Type DataSet, (Is it a DataSource/TSQLQuery?) so it would be something like DataModuleRules.SQLSelectRulesList.CopyFromDataSet(DataModuleRules.SQL SelectRulesList.DataSet, True); ---------------------------------------------------------------------- TheNick · 2021-05-26 11:10 Quote from: Zvoni on May 26, 2021, 11:25:22 amIf i read it right, your SQLSelectRulesList is already of Type DataSet, (Is it a DataSource/TSQLQuery?) so it would be something like DataModuleRules.SQLSelectRulesList.CopyFromDataSet(DataModuleRules.SQL SelectRulesList.DataSet, True); True, it is an TSQLQuery. I will try, what you suggested, in my other copydata-procedures. Thanks, Niclas