?
Solved

Runtime calculated fields

Posted on 1998-10-28
2
Medium Priority
?
386 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 20 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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