Solved

Runtime calculated fields

Posted on 1998-10-28
2
379 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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now