Solved

Runtime calculated fields

Posted on 1998-10-28
2
385 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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