Board index » delphi » How to create a duplicate of an TTable at runtime

How to create a duplicate of an TTable at runtime

Hi All

How can i duplicate ttable object to new ttable that i create at
runtime.
My function need to duplicate source ttable to new created ttable.

for example:

Function duplicate table(src_Tbl:TTable):TTable;
var
   tbl:TTable;
begin
    tbl:=ttable.Create(Aplication);
   .
   .
   how i can assign src_tbl to tbl ???
   .
   result:=Tbl;
end;
thanks :-)

Shraga Milon

 

Re:How to create a duplicate of an TTable at runtime


On Tue, 09 Jan 2001 13:00:52 +0200, Shraga Milon

Quote
<shrag...@internet-zahav.net> wrote:
>How can i duplicate ttable object to new ttable that i create at
>runtime.
>My function need to duplicate source ttable to new created ttable.

You didn't mention the database that you're using, but this should
work for Paradox and dBase:

  procedure DupTableStruct(SrceTbl: TTable; DestTblName: String);
  var
    TblDesc: CRTblDesc;
    PtrFldDesc, PtrIdxDesc, PtrVChkDesc, PtrRIntDesc: Pointer;
    CursorProp: CURProps;
    ItrVChk, ItrRInt: Integer;
  begin
    SrceTbl.Open;
    Check(DbiGetCursorProps(SrceTbl.Handle, CursorProp));
    // Allocate memory for field descriptors
    PtrFldDesc := AllocMem(SizeOf(FLDDesc) * CursorProp.iFields);
    PtrIdxDesc := AllocMem(SizeOf(IDXDesc) * CursorProp.iIndexes);
    PtrVChkDesc := AllocMem(SizeOf(VCHKDesc) * CursorProp.iValChecks);
    PtrRIntDesc :=
      AllocMem(SizeOf(RINTDesc) * CursorProp.iRefIntChecks);
    try
      Check(DbiGetFieldDescs(SrceTbl.Handle, PtrFldDesc));
      Check(DbiGetIndexDescs(SrceTbl.Handle, PtrIdxDesc));
      for ItrVChk := 1 to CursorProp.iValChecks do
        Check(DbiGetVChkDesc(SrceTbl.Handle, ItrVChk,

Pointer(Integer(PtrVChkDesc)+(ItrVChk-1)*SizeOf(VCHKDesc))));
      for ItrRInt := 1 to CursorProp.iRefIntChecks do
        Check(DbiGetRIntDesc(SrceTbl.Handle, ItrRInt,

Pointer(Integer(PtrRIntDesc)+(ItrRInt-1)*SizeOf(RINTDesc))));
      FillChar(TblDesc, SizeOf(TblDesc), #0);
      with TblDesc do
      begin
        StrPCopy(szTblName, DestTblName);
        StrCopy(szTblType, CursorProp.szTableType);
        iFldCount := CursorProp.iFields;
        pfldDesc := PtrFldDesc;
        iIdxCount := CursorProp.iIndexes;
        pIdxDesc := PtrIdxDesc;
        iValChkCount := CursorProp.iValChecks;
        pVChkDesc := PtrVChkDesc;
        iRIntCount := CursorProp.iRefIntChecks;
        pRIntDesc := PtrRIntDesc;
      end;
      Check(DbiCreateTable(SrceTbl.Database.Handle, True, TblDesc));
    finally
      FreeMem(PtrFldDesc);
      FreeMem(PtrIdxDesc);
      FreeMem(PtrVChkDesc);
      FreeMem(PtrRIntDesc);
    end;
  end;

HTH,

Jan

Re:How to create a duplicate of an TTable at runtime


Hi Jan

I use paradox database.
I need only to duplicate the TTable  object at runtime and not to create
new table.
Any help ?

Thanks  :-)

Shraga

Quote
Jan Sprengers wrote:
> On Tue, 09 Jan 2001 13:00:52 +0200, Shraga Milon
> <shrag...@internet-zahav.net> wrote:

> >How can i duplicate ttable object to new ttable that i create at
> >runtime.
> >My function need to duplicate source ttable to new created ttable.

> You didn't mention the database that you're using, but this should
> work for Paradox and dBase:

>   procedure DupTableStruct(SrceTbl: TTable; DestTblName: String);
>   var
>     TblDesc: CRTblDesc;
>     PtrFldDesc, PtrIdxDesc, PtrVChkDesc, PtrRIntDesc: Pointer;
>     CursorProp: CURProps;
>     ItrVChk, ItrRInt: Integer;
>   begin
>     SrceTbl.Open;
>     Check(DbiGetCursorProps(SrceTbl.Handle, CursorProp));
>     // Allocate memory for field descriptors
>     PtrFldDesc := AllocMem(SizeOf(FLDDesc) * CursorProp.iFields);
>     PtrIdxDesc := AllocMem(SizeOf(IDXDesc) * CursorProp.iIndexes);
>     PtrVChkDesc := AllocMem(SizeOf(VCHKDesc) * CursorProp.iValChecks);
>     PtrRIntDesc :=
>       AllocMem(SizeOf(RINTDesc) * CursorProp.iRefIntChecks);
>     try
>       Check(DbiGetFieldDescs(SrceTbl.Handle, PtrFldDesc));
>       Check(DbiGetIndexDescs(SrceTbl.Handle, PtrIdxDesc));
>       for ItrVChk := 1 to CursorProp.iValChecks do
>         Check(DbiGetVChkDesc(SrceTbl.Handle, ItrVChk,

> Pointer(Integer(PtrVChkDesc)+(ItrVChk-1)*SizeOf(VCHKDesc))));
>       for ItrRInt := 1 to CursorProp.iRefIntChecks do
>         Check(DbiGetRIntDesc(SrceTbl.Handle, ItrRInt,

> Pointer(Integer(PtrRIntDesc)+(ItrRInt-1)*SizeOf(RINTDesc))));
>       FillChar(TblDesc, SizeOf(TblDesc), #0);
>       with TblDesc do
>       begin
>         StrPCopy(szTblName, DestTblName);
>         StrCopy(szTblType, CursorProp.szTableType);
>         iFldCount := CursorProp.iFields;
>         pfldDesc := PtrFldDesc;
>         iIdxCount := CursorProp.iIndexes;
>         pIdxDesc := PtrIdxDesc;
>         iValChkCount := CursorProp.iValChecks;
>         pVChkDesc := PtrVChkDesc;
>         iRIntCount := CursorProp.iRefIntChecks;
>         pRIntDesc := PtrRIntDesc;
>       end;
>       Check(DbiCreateTable(SrceTbl.Database.Handle, True, TblDesc));
>     finally
>       FreeMem(PtrFldDesc);
>       FreeMem(PtrIdxDesc);
>       FreeMem(PtrVChkDesc);
>       FreeMem(PtrRIntDesc);
>     end;
>   end;

> HTH,

> Jan

Re:How to create a duplicate of an TTable at runtime


Function duplicate table(src_Tbl:TTable):TTable;
var
   tbl:TTable;
begin
    tbl:=ttable.Create(Aplication);
    with tbl do
    begin
      tbl.DataBaseName := srctbl.DatabaseName;
      tbl.TableName := srctbl.DatabaseName;
      Open;

--
Bill

Re:How to create a duplicate of an TTable at runtime


On Tue, 09 Jan 2001 21:00:01 +0200, Shraga Milon

Quote
<shrag...@internet-zahav.net> wrote:
>I need only to duplicate the TTable  object at runtime and not to create
>new table.

Sorry that I misunderstood your question.

There isn't really a trick about this: you have to copy over all the
relevant properties of the TTable like:
  SessionName
  DatabaseName
  TableName
  IndexName
  Filter
  Filtered
  etc.

If your event handlers rely on the Sender object rather than on the
hardcoded TTable name, you can copy them too.

Jan

Re:How to create a duplicate of an TTable at runtime


Thanks Jan

It is  work ok  now.

Shraga :-)

Quote
Jan Sprengers wrote:
> On Tue, 09 Jan 2001 21:00:01 +0200, Shraga Milon
> <shrag...@internet-zahav.net> wrote:

> >I need only to duplicate the TTable  object at runtime and not to create
> >new table.

> Sorry that I misunderstood your question.

> There isn't really a trick about this: you have to copy over all the
> relevant properties of the TTable like:
>   SessionName
>   DatabaseName
>   TableName
>   IndexName
>   Filter
>   Filtered
>   etc.

> If your event handlers rely on the Sender object rather than on the
> hardcoded TTable name, you can copy them too.

> Jan

Other Threads