Solved

Runtime calculated fields

Posted on 1998-10-28
2
381 Views
Last Modified: 2010-05-18
I'm constructing a table at Runtime and want to use a calculated field to find the product of two other fields in the table.

Anyone tell me how to set up the field etc at runtime. Thanks.
0
Comment
Question by:cavalry
2 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 10 total points
ID: 1344865
Hi cavalry,

 This should do :

Table1.Active:=false; // Must be false to add Fields to the Dataset
Table1.FieldDefs.Update; // Make sure we have the latest Fields
Monthly:=TFloatField.Create(Table1);
Monthly.FieldName:='Monthly Salary';
Monthly.Calculated:=true;
Monthly.Dataset:=Table1;
Monthly.DisplayFormat:='$#,0.00';
Table1.Active:=true;

Regards, Zif.

ps. don't forget to assign a procedure to OnFieldsCalc event
0
 
LVL 3

Expert Comment

by:rickpet
ID: 1344866
This ones a little more robust...taken from Ray Konopka's book
Custom Delphi 3 Components...highly recommeded...

function CreateCalcField(const FieldName: string; FieldClass: TFieldClass;
                                 Size: Word): TField;
begin
  //Taken from TRzBusinessComponent
  Result := FDataset.FindField(FieldName);
  if Result = nil then begin
    if FieldClass = nil then
      exit;
    Result := FieldClass.Create(Owner);
    try
      Result.FieldName := FieldName;
      if (Result is TStringField) or (Result is TBytesField) or
         (Result is TVarBytesField) then
        Result.Size := Size;
      Result.Calculated := True;
      Result.DataSet := FDataSet;
      Result.Name := GenerateNewFieldName(Owner, FDataset.Name, FieldName);
    except
      Result.Free;
      raise;
    end;
  end;
end;

function GeneratenewFieldName(AOwner: TComponent; const DatasetName: string;
                              const FieldName: string): string;
var
  i: Integer;
  NewName: string;
  Done: Boolean;
function ComponentExists(AOwner: TComponent; const CompName: string): boolean;
var
 i: Integer;
begin
  Result := False;
  for i := 0 to AOwner.ComponentCount -1 do begin
    if AnsiCompareText(CompName, AOwner.Components[i].Name) = 0 then begin
      Result := True;
      Break;
    end;
  end;    // for
end;
begin
  NewName := DatasetName;
  for i := 0 to Length (FieldName) do begin
    if FieldName[i] in ['0'..'9', '_', 'A'..'Z', 'a'..'z'] then
      NewName := NewName + FieldName[i];
  end;
  if ComponentExists(Owner, NewName) then begin
    i := 1;
    Done := False;
    repeat
      inc(i);
      if not ComponentExists(AOwner, Newname+IntToStr(i)) then begin
        Result := NewName + IntToStr(i);
        Done := True;
      end;
    until
      Done;
  end else
    Result := NewName;
end;


Rick
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question