Problem with copy lazarus / 16-Databases ---------------------------------------------------------------------- WTDdallas · 2016-06-18 17:17 I have written this code : procedure AppendRecord(dsSource, dsTarget: tdataset);     // Append Record  and copy from one Dataset to another var     i: Integer;                                                             // Requires both Dataset to be open begin   dsTarget.append;                                                          // Append target   with dsSource do   begin     for i := 0 to FieldCount-1 do                                           // For each field       begin        try         dsTarget.fieldbyname(fields.FieldName).value := Fields.Value;   // copy Source.Value to Target(source.fieldname).Value        finally        end;       end;   end;   dsTarget.post;                                                           // Post Target end;  I get invalid value,if the a date, crashes on empty date field. How do i Precheck for valid value?  How do i Fix that? ---------------------------------------------------------------------- Ñuño_Martínez · 2016-06-18 20:20 Please: use [ code=pascal] [ /code] (the icon with a # over the smileys) to format your code snippets.  They'll be easier to read. Also, "WITH" isn't very recommendable and you should use it only if you really need it (wich means almost never). It may introduce a lot of hard to track bugs.  And you don't need teh "TRY .. FINALLY .. END" block, as "FINALLY" is empty. Since you're not telling to the DataSet what field are you using, it tries (and success) to assign all fields to the target, as "Value" is of type "VARIANT".  You should use property "asDateTime" (or "asString", or "asInteger", or ...) instead of "Value". So: 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 (* Append Record  and copy from one Dataset to another *)PROCEDURE AppendRecord (dsSource, dsTarget:  TDataSet);VAR  i: INTEGER;BEGIN  dsTarget.Append;  FOR i := 0 TO FieldCount-1 DO    dsTarget.FieldByName (dsSource.Fields[i].FieldName).asDateTime :=    dsSource.Fields[i].asDateTime;  dsTarget.PostEND; ---------------------------------------------------------------------- WTDdallas · 2016-06-18 21:59 Quote from: Ñuño_Martínez on June 18, 2016, 10:20:36 pmPlease: use [ code=pascal] [ /code] (the icon with a # over the smileys) to format your code snippets.  They'll be easier to read. Also, "WITH" isn't very recommendable and you should use it only if you really need it (wich means almost never). It may introduce a lot of hard to track bugs.  And you don't need teh "TRY .. FINALLY .. END" block, as "FINALLY" is empty. Since you're not telling to the DataSet what field are you using, it tries (and success) to assign all fields to the target, as "Value" is of type "VARIANT".  You should use property "asDateTime" (or "asString", or "asInteger", or ...) instead of "Value". So: 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 (* Append Record  and copy from one Dataset to another *)PROCEDURE AppendRecord (dsSource, dsTarget:  TDataSet);VAR  i: INTEGER;BEGIN  dsTarget.Append;  FOR i := 0 TO FieldCount-1 DO    dsTarget.FieldByName (dsSource.Fields[i].FieldName).asDateTime :=    dsSource.Fields[i].asDateTime;  dsTarget.PostEND; The Hold Idea is to setup a generic Append record, so you don't know which fields is what, and don't have to. I was just needed a way to trap the error and skip to next field. This worked until A non-date value was import into the dataset, which I fix on import, did prechecking there. But I want to make a Smarter Append Record, So it can't crash, or at least skips field. Sorry didn't know about code# button. 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 [/procedure AppendRecord(dsSource, dsTarget: tdataset);                         // Append Record  and copy from one Dataset to anothervar    i: Integer;                                                             // Requires both Dataset to be openbegin  dsTarget.append;                                                          // Append target   with dsSource do  begin    for i := 0 to FieldCount-1 do                                           // For each field      begin       try      dsTarget.fieldbyname(fields[i].FieldName).value := Fields[i].Value;   // copy Source.Value to Target(source.fieldname).Value       finally     end;      end;  end;   dsTarget.post;                                                           // Post Targetend; procedure ReplaceRecord(dsSource, dsTarget: tdataset);                       // Copy Record from one Dataset to another over existing onevar    i: Integer;                                                              // Requires both Dataset to be openbegin  dsTarget.edit;                                                            // Append target   with dsSource do  begin    for i := 0 to FieldCount-1 do                                           // For each field      begin        try        dsTarget.fieldbyname(fields[i].FieldName).value := Fields[i].Value;   // copy Source.Value to Target(source.fieldname).Value         finally        end;      end;  end;   dsTarget.post;                                                            // Post Targetend;      ]