Recent

Author Topic: Problem with copy  (Read 2468 times)

WTDdallas

  • Full Member
  • ***
  • Posts: 100
Problem with copy
« on: June 18, 2016, 07:17:46 pm »
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

  • Hero Member
  • *****
  • Posts: 1226
    • Burdjia
Re: Problem with copy
« Reply #1 on: June 18, 2016, 10:20:36 pm »
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][+][-]
  1. (* Append Record  and copy from one Dataset to another *)
  2. PROCEDURE AppendRecord (dsSource, dsTarget:  TDataSet);
  3. VAR
  4.   i: INTEGER;
  5. BEGIN
  6.   dsTarget.Append;
  7.   FOR i := 0 TO FieldCount-1 DO
  8.     dsTarget.FieldByName (dsSource.Fields[i].FieldName).asDateTime :=
  9.       dsSource.Fields[i].asDateTime;
  10.   dsTarget.Post
  11. END;  
  12.  
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

WTDdallas

  • Full Member
  • ***
  • Posts: 100
Re: Problem with copy
« Reply #2 on: June 18, 2016, 11:59:19 pm »
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][+][-]
  1. (* Append Record  and copy from one Dataset to another *)
  2. PROCEDURE AppendRecord (dsSource, dsTarget:  TDataSet);
  3. VAR
  4.   i: INTEGER;
  5. BEGIN
  6.   dsTarget.Append;
  7.   FOR i := 0 TO FieldCount-1 DO
  8.     dsTarget.FieldByName (dsSource.Fields[i].FieldName).asDateTime :=
  9.       dsSource.Fields[i].asDateTime;
  10.   dsTarget.Post
  11. END;  
  12.  

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][+][-]
  1. [/
  2. procedure AppendRecord(dsSource, dsTarget: tdataset);                         // Append Record  and copy from one Dataset to another
  3. var
  4.     i: Integer;                                                               // Requires both Dataset to be open
  5. begin
  6.   dsTarget.append;                                                            // Append target
  7.  
  8.   with dsSource do
  9.   begin
  10.     for i := 0 to FieldCount-1 do                                             // For each field
  11.       begin
  12.        try
  13.         dsTarget.fieldbyname(fields[i].FieldName).value := Fields[i].Value;   // copy Source.Value to Target(source.fieldname).Value
  14.        finally
  15.        end;
  16.       end;
  17.   end;
  18.  
  19.   dsTarget.post;                                                             // Post Target
  20. end;
  21.  
  22. procedure ReplaceRecord(dsSource, dsTarget: tdataset);                       // Copy Record from one Dataset to another over existing one
  23. var
  24.     i: Integer;                                                              // Requires both Dataset to be open
  25. begin
  26.   dsTarget.edit;                                                              // Append target
  27.  
  28.   with dsSource do
  29.   begin
  30.     for i := 0 to FieldCount-1 do                                             // For each field
  31.       begin
  32.         try
  33.         dsTarget.fieldbyname(fields[i].FieldName).value := Fields[i].Value;   // copy Source.Value to Target(source.fieldname).Value
  34.  
  35.         finally
  36.         end;
  37.       end;
  38.   end;
  39.  
  40.   dsTarget.post;                                                              // Post Target
  41. end;
  42.      
  43. ]
« Last Edit: June 19, 2016, 12:31:01 am by WTDdallas »

 

TinyPortal © 2005-2018