Solved

Runtime calculated fields

Posted on 1998-10-28
2
383 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Seattle: Project Options : Version info for all platforms, all releases? 2 112
Delphi application Soap connection 5 130
Delphi 2 77
Delphi and Access based Enumeration 9 77
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

680 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