There may be a handy ADO facility, but I'm not aware of one.
I use the funtion below, which you're welcome to.
Have fun
Simon
type
TCopyRecordOption=(crReturnToOriginal,crReturnNewAutoincValue);
TCopyRecordOptions=set of TCopyRecordOption;
TAction=(crCopy,crIgnore,crSet,crAutoinc);
function CopyRecord(ADataset:TDataset; AIgnoreFields,ASetFields:array of string;
ASetValues:array of variant; AOptions:TCopyRecordOptions=[]):integer;
var
i,q:integer;
LAction:array of TAction;
LValues:array of variant;
LBookmark:TBookmark;
LAutoincFieldIndex:integer;
begin
Result:=0;
LAutoincFieldIndex:=-1;
with ADataset do begin
{Provide room in arrays}
SetLength(LAction,Fields.Count);
SetLength(LValues,Fields.Count);
if(crReturnToOriginal in AOptions)then
LBookmark:=GetBookmark
else
LBookmark:=nil;
try
{Note existing values and what to do with each field}
for i:=0 to Fields.Count-1 do
with Fields do
if(ClassType=TAutoIncField)then begin
if(crReturnNewAutoincValue in AOptions)and
(LAutoincFieldIndex=-1)then begin
LAutoincFieldIndex:=i;
LAction:=crAutoInc;
end else
LAction:=crIgnore;
end else
if(ReadOnly)or
(InTextList(FieldName,AIgnoreFields)<>-1)or
(ClassType=TDatasetField)then
LAction:=crIgnore
else begin
q:=InTextList(FieldName,ASetFields);
if(q<>-1)then begin
LAction:=crSet;
LValues:=ASetValues[q]; {Alternative is to find q later on}
end else begin
LAction:=crCopy;
LValues:=Value;
end;
end;
{Create new record}
Append;
{Put in values}
for i:=0 to Fields.Count-1 do
case LActionof
crCopy,crSet:Fields.Value:=LValues;
end;{case}
{Post; No post. Can hide record if filtered or a detail recordset}
{Note new autoindex field value if required}
if(crReturnNewAutoincValue in AOptions)and
(LAutoincFieldIndex>=0)then
Result:=Fields[LAutoincFieldIndex].asInteger;
finally
if(crReturnToOriginal in AOptions)then begin
GotoBookmark(LBookmark);
FreeBookmark(LBookmark);
end;
end;
end;
end;
Nice LucieLastic! I didn't think of using sql for doing this which is rather stupid as my main experience with databases so far is MySQL / PHP.
The table is not that big so it is not a problem to manually enter the field names. However it would be interesting to know how to extract the field names through sql.
It seems you cannot just go ahead with the sql query. It raises errors for fields with no value. So you still have to go through the source record and make sure only fields with a value are included in the insert query, correct?
I also wonder if you can return the ID of the new record from the query? In php you simple use the function mysql_insert_id(). Is there anything like that in Delphi?
"It raises errors for fields with no value"
Why would it raise an error here, are you talking about NULL fields? If the field is null, it will write null to the new record. I've used this type of query quite a few times in the past, but I must admit never thru ADO, I've always done it straight on the database.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.