Jacek Laskowski 57 Posted November 19, 2020 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
Guest Posted November 20, 2020 (edited) 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 November 20, 2020 by Guest Share this post Link to post
Jacek Laskowski 57 Posted November 20, 2020 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
Guest Posted November 22, 2020 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