Board index » delphi » Copying records from one table to another?

Copying records from one table to another?

(Sorry to have posted this previously without a subject line.)

I have a DBase IV table from which I want to allow the user to delete
records. But I want them put into a "DEAD" file for possible future
recovery. I haven't been able to find a function or method that will copy
a single record. I've seen batch commands and field-by-field commands.
Does DELPHI have a command that will allow one to copy and delete an
entire single record to another table that has an identical field
structure? I'm using Delphi's native database capability.

Thanks,
Joe Dlhopolsky
joed...@i-2000.com

 

Re:Copying records from one table to another?


In article: <44ro5p$...@i-2000.com>  Joe Dlhopolsky <joed...@i-2000.com> writes:

Quote

> (Sorry to have posted this previously without a subject line.)

> I have a DBase IV table from which I want to allow the user to delete
> records. But I want them put into a "DEAD" file for possible future
> recovery. I haven't been able to find a function or method that will copy
> a single record. I've seen batch commands and field-by-field commands.
> Does DELPHI have a command that will allow one to copy and delete an
> entire single record to another table that has an identical field
> structure? I'm using Delphi's native database capability.

> Thanks,
> Joe Dlhopolsky
> joed...@i-2000.com

It is not as neat as I would like it yet, but the following function copies the current record from srcTBL to tgtTbl. If
RecCreate is True then a new record is created in the target table otherwise the current record is written to.

It does not require that the tables are the same strutcure but does require that the fields you want to copy hav
ethe same name (and the same type!). Both tables have to be on the form and open.

the function returns a count of the number of fields used..might be useful ?

Hope this helps.

function DbsCopyRecord(srcTbl, tgtTbl : TTable; RecCreate : Boolean) : Integer;
var
    i, c, added : integer;
    SFldNames, TFldNames: TStringList;
    FldExist: Boolean;
 begin

   Result := 0;

   try
     SFldNames := TStringList.Create;      {open the list for field names}
     srcTbl.GetFieldNames(SFldNames);      {get the fieldnames into the list}

     TFldNames := TStringList.Create;      {open the list for field names}
     tgtTbl.GetFieldNames(TFldNames);      {get the fieldnames into the list}

     tgtTbl.Edit;                           {set target into edit mode}

     if RecCreate then tgtTbl.Append;      {add a new record if required}

     {now copy the values from Source to Target.}

     for i := 0 to (SFldNames.Count - 1) do begin

        FldExist := False;

        for c := 0  to (TFldNames.Count - 1) do begin

          if (UpperCase(SFldNames[i]) = UpperCase(TFldNames[c])) then begin
             FldExist := True;
             Break;
          end;
        end;

        if FldExist then begin
           tgtTbl.FieldByName(SFldNames[i]).Assign(srcTbl.FieldByName(SFldNames[i]));
           inc(Result);
        end;
     end;
   finally
     tgtTbl.Post;                         {post the txn record}
     SFldNames.Free;                     {free the stringlist from memory}
     TFldNames.Free;
   end;

   end;

--
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Russell Weetch
http://www.ukindex.co.uk/
** HAVE YOU REGISTERED YOUR WEB SITE? **
'quite exciting this computer magic'
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Re:Copying records from one table to another?


In article <457151574...@weetch.demon.co.uk>, Russ...@weetch.demon.co.uk
says...
Quote

>In article: <44ro5p$...@i-2000.com>  Joe Dlhopolsky <joed...@i-2000.com>
writes:

>> (Sorry to have posted this previously without a subject line.)

>> I have a DBase IV table from which I want to allow the user to delete
>> records. But I want them put into a "DEAD" file for possible future
>> recovery. I haven't been able to find a function or method that will copy
>> a single record. I've seen batch commands and field-by-field commands.
>> Does DELPHI have a command that will allow one to copy and delete an
>> entire single record to another table that has an identical field
>> structure? I'm using Delphi's native database capability.

>> Thanks,
>> Joe Dlhopolsky
>> joed...@i-2000.com

>It is not as neat as I would like it yet, but the following function copies

the current record from srcTBL to tgtTbl. If
Quote

>RecCreate is True then a new record is created in the target table otherwise

the current record is written to.
Quote

>It does not require that the tables are the same strutcure but does require

that the fields you want to copy hav

- Show quoted text -

Quote
>ethe same name (and the same type!). Both tables have to be on the form and
open.

>the function returns a count of the number of fields used..might be useful ?

>Hope this helps.

>function DbsCopyRecord(srcTbl, tgtTbl : TTable; RecCreate : Boolean) :
Integer;
>var
>    i, c, added : integer;
>    SFldNames, TFldNames: TStringList;
>    FldExist: Boolean;
> begin

>   Result := 0;

>   try
>     SFldNames := TStringList.Create;      {open the list for field names}
>     srcTbl.GetFieldNames(SFldNames);      {get the fieldnames into the list}

>     TFldNames := TStringList.Create;      {open the list for field names}
>     tgtTbl.GetFieldNames(TFldNames);      {get the fieldnames into the list}

>     tgtTbl.Edit;                           {set target into edit mode}

>     if RecCreate then tgtTbl.Append;      {add a new record if required}

>     {now copy the values from Source to Target.}

>     for i := 0 to (SFldNames.Count - 1) do begin

>        FldExist := False;

>        for c := 0  to (TFldNames.Count - 1) do begin

>          if (UpperCase(SFldNames[i]) = UpperCase(TFldNames[c])) then begin
>             FldExist := True;
>             Break;
>          end;
>        end;

>        if FldExist then begin

tgtTbl.FieldByName(SFldNames[i]).Assign(srcTbl.FieldByName(SFldNames[i]));
Quote
>           inc(Result);
>        end;
>     end;
>   finally
>     tgtTbl.Post;                         {post the txn record}
>     SFldNames.Free;                     {free the stringlist from memory}
>     TFldNames.Free;
>   end;

>   end;

>--

When you do know the number of fields in the involved database tables you could
also do something like this:

Table2.InsertRecord([Table1.Fields[0], Table1.Fields[1], {etc. up to the number
of fields involved}]);

kind regards,

Maarten    

Other Threads