以下函式能讓你複製一筆記錄:
(*
* 從資料表 Source 拷貝一筆記錄至 Destination
* Destination 必須為 Edit 或 Insert 狀態
* 複製每個欄位時會自動偵測目的資料表中是否存在該欄位
* SkipFields 參數用來指定不要複製的欄位 (例如: PrimaryKey 欄位)
*
* Example: DbCopyRecord(Table1, Table2, 'CUST_NO;CREA_DATE');
*)
procedure DbCopyRecord(Source: TBDEDataSet; Destination: TTable;
const SkipFields: String);
var
i: integer;
FldName: string;
slSkipFields: TStrings;
begin
if not (Destination.State in dsEditModes) then
raise Exception.Create('資料集不是編輯狀態!');
slSkipFields := TStringList.Create;
try
StrTokenToStrings(UpperCase(SkipFields), ';', slSkipFields);
for i := 0 to Source.FieldCount-1 do
begin
FldName := Source.Fields[i].FieldName;
if slSkipFields.IndexOf(UpperCase(FldName)) >= 0 then
continue;
// 忽略目的資料集中沒有的欄位
if Destination.FindField(FldName) = nil then
continue;
// 忽略唯讀的欄位
if Destination.FieldByName(FldName).ReadOnly then
continue;
Destination.FieldbyName(FldName).Value := Source.Fields[i].Value;
end;
finally
slSkipFields.Clear;
slSkipFields.Free;
end;
end;
               (
geocities.com/huanlin_tsai)