Jump to content
Sign in to follow this  
Jacek Laskowski

Adding lookup fields to active dataset, how?

Recommended Posts

Is there any way to add a lookup or calculated column to an open dataset? Normally a dataset needs to be closed, but in my case I lose data (this is TFDMemTable), so I would like to add columns on the active dataset.
I will add that lookup fields are not data fields and are not included in the data buffer, so there should be some way to add them.

Share this post


Link to post
5 hours ago, Jacek Laskowski said:

Is there any way to add a lookup or calculated column to an open dataset? Normally a dataset needs to be closed, but in my case I lose data (this is TFDMemTable), so I would like to add columns on the active dataset.
I will add that lookup fields are not data fields and are not included in the data buffer, so there should be some way to add them.

try this tip:

 

  // ***************************************************************************
  // ********* for you dont lost your records (data) when forget save and close your FDMemTables:
  // where the data will be saved!
  FDMemTable1.ResourceOptions.PersistentFileName := 'myData.xml';
  // save all data when you close your Dataset!
  // now, you can close your dataset and your data dont will be losted!
  FDMemTable1.ResourceOptions.Persistent := true;
  //
var
  lMyFieldLookUp: TField;
begin
  // ******************** add new field type: lookup ****************************
  // dont forget: TRY EXCEPT END and TRY FINALLY END is good pratice in this case!
  lMyFieldLookUp           := TField.Create(FDMemTable1);
  lMyFieldLookUp.FieldName := 'MyFieldLookUp';
  //
  lMyFieldLookUp.KeyFields         := 'ID'; // or 'A;B;C...' // from FDMemTable1
  lMyFieldLookUp.LookupKeyFields   := 'ID'; // or 'A;B;C...' // from FDMemTable2
  lMyFieldLookUp.LookupResultField := 'Names';
  lMyFieldLookUp.FieldKind         := TFieldKind.fkLookup;
  lMyFieldLookUp.LookupDataSet     := FDMemTable2;
  lMyFieldLookUp.Visible           := true;
  //
  FDMemTable1.Close;
  //
  FDMemTable1.Fields.Add(lMyFieldLookUp);
  //
  FDMemTable1.Open;
  // ***************************************************************************
 ...

hug

Edited by emailx45

Share this post


Link to post

This is not the solution I am looking for, I can save and load a dataset content without such "tricks".

I'm interested in ADDING a new lookup field with an ACTIVE dataset.

Share this post


Link to post
On 11/20/2020 at 4:32 PM, Jacek Laskowski said:

This is not the solution I am looking for, I can save and load a dataset content without such "tricks".

I'm interested in ADDING a new lookup field with an ACTIVE dataset.

Can a doctor operate on his patient without anesthesia? Yes!
But what if the patient runs out of the operating room?
It would not be better to tie it to the table, or, to sedate it totally!


So, the object was already pre-compiled during the software creation process. Your limits have already been set, how do you expect this to be allowed to change?

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
Sign in to follow this  

×