Skip to content

HowToProgressiveField

Carlo Barazzetta edited this page Jan 20, 2017 · 1 revision

How to calculate an incremental field

If you want to autogenerate an incremental value for a field, you have to:

  • declare and write a rule for the model in Delphi rule.pas unit
  • define the node Rules: for the model

Example from a real project:

type
  TCalcProgressivoIntervento = class(TKRuleImpl)
  public
    procedure BeforeAdd(const ARecord: TKRecord); override;
  end;

procedure TCalcProgressivoIntervento.BeforeAdd(const ARecord: TKRecord);
var
  LDBConnection: TEFDBConnection;
  LQuery: TEFDBQuery;
  LAnnoIntervento: string;
begin
  LDBConnection := TKConfig.Instance.DBConnections[TKConfig.Instance.DatabaseName];
  LQuery := LDBConnection.CreateDBQuery;
  try
    LQuery.CommandText := 'SELECT MAX(CAST(RIGHT(INTERVENTO.ID,5) AS INTEGER)) MAXID FROM INTERVENTO';
    LQuery.Open;
    LAnnoIntervento := FormatDateTime('yy', ARecord.FieldByName('DataIntervento').AsDate);
    ARecord.FieldByName('Id').AsString := LAnnoIntervento +
      PadLeft(IntToStr(LQuery.DataSet.FieldByName('MAXID').AsInteger + 1),5);
  finally
    FreeAndNil(LQuery);
  end;
end;
ModelName: Intervento
...
Rules:
  CalcProgressivoIntervento:
  InterventoCheck:
Clone this wiki locally