How to use code in Delphi to complete the creation of the calculation field

zhaozj2021-02-16  51

There were many people in the past 9CBS asked this problem, but it seems that no one gives a satisfactory answer. I also have a long time to find the answer, now I will share it with you now: procedure tform1.form; var newfield: tfield; i: integer; begin // table has two fields Sname, Birth, now We dynamically generate a calculation field AGE showing age newfield: = tstringfield.create (adotable); // Create a TStringField type field adotable.close; for i: = 0 to adotable.fields.count-1 do adotable.fields [0] .free; // Release all static field for i: = 0 to adotable.fielddefs.count-1 do adotable.fielddefs.Items [i] .createfield (adotable); // According to field information according to Fielddefs Generate static fields newfield.size: = 5; newfield.fieldname: = 'agn'; newfield.fieldkind: = fkcalculated; // Set this field to calculate field newfield.DataSet: = adotable; // Put this field Add this field Add this field Add this field Add this field Add this field to ADOTABLE ADOTABLE.OPEN; END;

procedure TForm1.ADOTableCalcFields (DataSet: TDataSet); var YY1, YY2, MM, DD: Word; TmpDate: TDate; begin DecodeDate (Date, YY1, MM, DD); TmpDate:. = DataSet.FieldByName ( 'Birth') AsDateTime DECODEDATE (TMPDATE, YY2, MM, DD); DataSet.fieldByname ('agn'). Asstring: = INTOSTR (YY1-YY2) 'year'; // Show age end in oncalfield; or more I use ADO written. At first I wrote in BDE, it can be passed.

转载请注明原文地址:https://www.9cbs.com/read-27974.html

New Post(0)