Delphi Examples: DbiSetFieldMap

<< Click to Display Table of Contents >>

Navigation:  »No topics above this level«

Delphi Examples: DbiSetFieldMap

Return to chapter overview

Set a field map for the current table.

Note:        Most Delphi users should use the Fields Editor of a TTable to set the field mapping.

This example uses the following input:

 fDbiSetFieldMap(CustomerTbl, [CustomerTbl.FieldByName('Company'),

   CustomerTbl.FieldByName('City')]);

 

The procedure is:

procedure fDbiSetFieldMap(Table: TTable; const Fields: array of TField);

var

 CurrentElement, Elements, FldNum: Integer;

 pFields, pOrigFields, pF, pOF: pFLDDesc;

begin

 Elements := sizeof(Fields) div sizeof(TField);

 pFields := AllocMem(Elements * sizeof(FLDDesc));

 pOrigFields := AllocMem(Table.FieldCount * sizeof(FLDDesc));

 pF := pFields;

try

   // Get the original field descriptors

   Check(DbiGetFieldDescs(Table.Handle, pOrigFields));

   // Iterate through the original fields and create a pFLDDesc structure

   //    for the new field map structure

  for CurrentElement := 0 to (Elements 1) do begin

     pOF := pOrigFields;

    for FldNum := 1 to Table.FieldCount do begin

       // Add only the field names that match

      if (StrIComp(PChar(Fields[CurrentElement].FieldName), pOF.szName) = 0)

      then begin

         // Move the original FLDDesc to the new FLDDesc

         move(pOF^, pF^, sizeof(FLDDesc));

         Inc(pF);

         break;

      end

      else

         Inc(pOF);

    end;

  end;

   Check(DbiSetFieldMap(Table.Handle, Elements, pFields));

finally

   FreeMem(pFields, Elements * sizeof(FLDDesc));

   FreeMem(pOrigFields, Table.FieldCount * sizeof(FLDDesc));

end;

end;