1

Using Delphi 10.2.3, I dropped in a form a Firedac TFDMemTable component which was I used Field Editor to define 5 fields, one of those fields is a Lookup Field (FieldKind = fkLookup) .

I created a code to scan all fields of the MemSource MemTable to add those fields in a MemDestination Memtable.

I had a problem in scan the MemSource.FieldDefs because its total number of fields did not consider the Lookup one ! See the code :

    procedure TFormA.BTCopyFieldsClick(Sender: TObject);
    var
       i         : integer;
    begin

      for I := 0 to MemSource.FieldDefs.Count-1 do
      begin
            if (memSource.Fields.Fields[i].FieldKind = FkData) then
                memDestination.FieldDefs.Add (
                         memSource.FieldDefs.Items[i].DisplayName,
                         memSource.FieldDefs.Items[i].DataType,
                         memSource.FieldDefs.Items[i].Size,
                         memSource.FieldDefs.Items[i].Required
                         )
           Else

                ShowMessage( GetEnumName(TypeInfo(TFieldKind),
                             Integer(memSource.FieldDefs.Items[i].Datatype)
                           );

           memDestination.CreateDataSet;
           memDestination.CopyDataSet(memSource,[coRestart, coAppend]);
      end;
   end;

I realized that memSource.FieldDefs.Count had 4 fields only, it missed the 5th one which was a lookup type .

However memSource.Fields.Count accounts for 5 fields, that is correct.

The questions are :

  1. Is lookup field defined at design time considered in FieldDefs ?
    (if it is not considered, so WHY ?)

  2. Why memSource.FieldDefs.Count is different from memSource.Fields.Count ? (i.e. I have more fields in the memtable than in its definition )

Am I doing somethinf wrong in the above code ? What ?

Thanks in advance !!

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
JRG
  • 513
  • 9
  • 23
  • 2
    FieldDefs are the underlying table fields and Fields are the Delphi side and can include lookups and calculated fields which are not part of the underlying database table. – Brian Jan 31 '19 at 14:18
  • 1
    This point has cropped up before and is answered here: https://stackoverflow.com/questions/46073142/cannot-add-calculation-field-to-clientdataset/48997060 – MartynA Jan 31 '19 at 19:07
  • @Brian, thanks for the explanation. I tested an inclusion of a calculated field in a TFDmemtable and this field appeared in the FieldDefs too, besides the fields of the related databse table. Do you know the reason why calculated fields appear in FieldDefs while lookup does not ? – JRG Feb 04 '19 at 22:25
  • @MartinA, thks for the thread reference. Based on that thread, do you know if it is possible to scan a dataset.fields collection and identify if a field is a lookup, calculated or table based ? – JRG Feb 04 '19 at 22:40

0 Answers0