Solved

Copy a dataset and its persistent fields at runtime

Posted on 2008-10-01
8
1,131 Views
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 :)
0
Comment
Question by:rfwoolf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 21

Expert Comment

by:ziolko
ID: 22618409
don't know this TZTable but I would try .Assign() method on fields collection

TTable has:
Table1.FieldList.Assign()
Table1.FieldDefs.Assign()


ziolko.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 22618700
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

ziolko.
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 22644672
Hmm it's telling me FieldList.Assign is a readonly property.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:rfwoolf
ID: 22644831
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.
0
 
LVL 13

Author Comment

by:rfwoolf
ID: 22644993
The answer appears to lie here from a Borland article:
http://groups.google.co.za/group/borland.public.delphi.objectpascal/browse_thread/thread/599c60dcac5cb7fd?pli=1
 

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 
http://www.borland.com/devsupport/delphi/ti/ 
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 
      begin 
        FieldName := 'TaxAmount'; 
        Calculated := True; 
        Currency := True; 
        DataSet := MyTable; 
        Name := MyTable.Name + FieldName; 
        MyTable.FieldDefs.Add(Name, ftFloat, 0, false); 
      end; 
    end; 
==========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: 
var 
    TaxAmount: TFloatField; 
but I ended up defining a generic TField like this: 
var 
    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, 
MyField.Required); 
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} 
end; 
 
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

0
 
LVL 13

Author Comment

by:rfwoolf
ID: 22645367
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.
0
 
LVL 13

Accepted Solution

by:
rfwoolf earned 0 total points
ID: 22645657
I finally came right after looking through some related posts on EE:
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_20553395.html
Ziolko thanks for trying though :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 136
How to debug For loops? 3 75
How to build JSON File in Delphi 6 3 96
Tvertscrollbox like a whatsapp layout delete messages 1 50
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 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