Copy a dataset and its persistent fields at runtime

Posted on 2008-10-01
Last Modified: 2012-05-05
I have a dataset (TZTable -- similar to other datasets like TTable)
where I have cretaed some persistent fields.

I need to make a copy of this table at runtime and only change 1 thing - the name of the copy.

Anybody know how I can copy including its persistent fields :)
Question by:rfwoolf
  • 5
  • 2
LVL 21

Expert Comment

Comment Utility
don't know this TZTable but I would try .Assign() method on fields collection

TTable has:

LVL 21

Expert Comment

Comment Utility
and if you want make complete clone including data, I guess easiest way would be assign all properties like database name, table name, session name etc. to new table object and then call .Open

LVL 13

Author Comment

Comment Utility
Hmm it's telling me FieldList.Assign is a readonly property.
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

LVL 13

Author Comment

Comment Utility
When I said
"I need to make a copy of this table at runtime and only change 1 thing - the name of the copy."
I meant I need a copy of this "TTable" at runtime. I am not trying to duplicate a database table, I am trying to duplicate a dataset.
LVL 13

Author Comment

Comment Utility
The answer appears to lie here from a Borland article:

I'll be glad to explain what I did. 

I don't have my actual code here, it's at work. 

But I started looking at 

under "Database Programming". 

The TI1467D 3/31/99 Dynamically creating a TTable & fields at runtime 

contained most of the solution. 

The interesting part was this code: 

==========quote begin========== 

{ Create a calculated TField, assign properties, 

      and add to MyTable's field definitions array. } 

      TaxAmount := TFloatField.Create(MyTable); 

      with TaxAmount do 


        FieldName := 'TaxAmount'; 

        Calculated := True; 

        Currency := True; 

        DataSet := MyTable; 

        Name := MyTable.Name + FieldName; 

        MyTable.FieldDefs.Add(Name, ftFloat, 0, false); 



==========quote end========== 

The TI contains some different lines for creating non calculated fields, but 

the above code snippet turns out to be able to create calculated as well as 

non calculated fields. 

The TI defines this: 


    TaxAmount: TFloatField; 

but I ended up defining a generic TField like this: 


    MyField: TField; 

So then I loop thorugh the SourceDataSet's Fields: 

  for i := 0 to SourceDataSet.FieldCount - 1 do 

and depending on the Fields DataType property I created the fields using 

      MyField := TFloatField.Create(MyTable); 

      MyField := TIntegerField.Create(MyTable); 

      MyField := TStringField.Create(MyTable); 

or whatever using a case statement. 

After creating the field, all that now remains is to set up the TField 

properties as desired (copying the properties from the 

SourceDataSet.Fields[i]), and as the last thing, add it to the table: 

        MyTable.FieldDefs.Add(MyField.Name, MyField.DataType, MyField.Size, 


To set up certain properties it's nescessary to use a case statement again: 

case SourceDataSet.Fields[i].DataType of 

  ftFloat: TFloatField(MyField).DisplayFormat := ',0'; 

{and so on} 


That's about it, as far as I remember. 

There is probably a lot of typos in this code, because I'm writing it from 

memory, so consider yourself warned! 

And maybe there is an easier way to do it, but this worked for me. 

Finn Tolderlund. 

Open in new window

LVL 13

Author Comment

Comment Utility
Auurghh... still not winning. and there's so little information on the net.
Looking at the DFM of my form with the TTable I see that all it has is "objects" with name, fieldtype, and 1 or 2 other properties.  I don't know if I should be updating FieldDefs or FieldLists or just adding objects..
I have tried 2 methods of cloning -- 1 is to copy to clipboard at runtime and paste but then the fields do not get copied -- 2 is to use a Stream and the fields still do not get copied.
LVL 13

Accepted Solution

rfwoolf earned 0 total points
Comment Utility
I finally came right after looking through some related posts on EE:
Ziolko thanks for trying though :)

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now