Jump to content
Jacek Laskowski

TFDMemTable - how to clear structure?

Recommended Posts

How to clear structure of TFDMemTable? Not only data like in EmptyDataset() method, but fields, indexes, filters, etc. too.

Is possible?

Share this post


Link to post

There are various ClearXxx methods for TFDMemTable, but it is not obvious which one(s) to call, or in which order, and if they actually reset the whole thing.
The above suggestions, on the other hand, are solid.

Share this post


Link to post

Hello Jaca! :)

There is no simple solution for that.

However, I would suggest basing your own solution on the CopyDataSet code.
Note that if you specify the coStructure parameter in the options of CopyDataSet method, you will get what you want.
Almost ;-)

Share this post


Link to post

Hello -

 

I'm having same/similar issue when I try to reuse a TFDMemTable to load varying column count csv files.

 

I read in the csv's header, construct the table fields then create the table. The second time it's run, I get a duplicate FieldDef error. 

 

fdmColumns is a TFDMemTable containing the list of column headings. fdmColumnValues is the TFDMemTable I want to load the contents of the csv file into. What am I missing?

 

Teri

 

  procedure DefineTable;
  begin
    fdmColumns.First;
    fdmColumnValues.Free;
    fdmColumnValues := TFDMemTable.Create(self);
    while not fdmColumns.eof do
    begin
      with fdmColumnValues.FieldDefs.AddFieldDef do
      begin
        Name := fdmColumns['ColumnName'];
        DataType := ftString;
      end;
      fdmColumns.Next;
    end;
    fdmColumnValues.CreateDataSet;
    dsColumnValues.DataSet := fdmColumnValues;
  end;

 

Share this post


Link to post

Clear not only the Fields property but the FieldDefs too.  Do this while the dataset is closed.  Then you can rebuild your new fielddefs.

 

  if FDMemTable1.Active then
  begin
    FDMemTable1.Close;
    FDMemTable1.Fields.Clear;
    FDMemTable1.FieldDefs.Clear;
  end;

 

Now it is ready for you to define the new columns.  Index etc done the same way, just clear their definitions.  Don't forget there are both an Indexes and an IndexDef, both should be cleared.  Filter you would just assign the empty string.

  • Like 1

Share this post


Link to post

Thanks for the responses. I was actually clearing the dataset fdmColumnValues correctly, by clearing Fields and FieldDefs, but I had neglected to empty the fdmColumns dataset which I was using to define the field names. Thus, I was getting a duplicate field error. DOH!

 

Teri

 

Share this post


Link to post

Hi

I'm having exactly the same problem - I've simplified my process for the purpose of this post - 

 

QryChart.Sql.Text := 'SELECT CHARTSERIES, TOTAL, CHARTDATE FROM ORDERS' ;

QryChart.Open ;

 

memChartData.Close ;
memChartData.Indexes.Clear;
memChartData.IndexDefs.Clear;

memChartData.Fields.Clear;
memChartData.FieldDefs.Clear;

memChartData.CopyDataSet(dmData.QryChart, [coStructure, coRestart, coAppend]);

 

So far, no problem. Then

 

QryChart.Close ;

QryChart.Sql.Text := 'SELECT CHARTSERIES, TOTAL FROM ORDERS' ;

QryChart.Open ;

 

memChartData.Close ;
memChartData.Indexes.Clear;
memChartData.IndexDefs.Clear;

memChartData.Fields.Clear;
memChartData.FieldDefs.Clear;

memChartData.CopyDataSet(dmData.QryChart, [coStructure, coRestart, coAppend]);

 

It fails on CopyDataSet with the message CHARTDATE object not found, so it clearly hasn't removed that field form the defs.

 

Thanks for any advice.

Andy

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×