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:
(* 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.Post
END;
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.
[/
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[i].FieldName).value := Fields[i].Value; // copy Source.Value to Target(source.fieldname).Value
finally
end;
end;
end;
dsTarget.post; // Post Target
end;
procedure ReplaceRecord(dsSource, dsTarget: tdataset); // Copy Record from one Dataset to another over existing one
var
i: Integer; // Requires both Dataset to be open
begin
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 Target
end;
]