Solved

Calulated Field?

Posted on 1999-01-09
13
400 Views
Last Modified: 2010-04-06
Can I use a Calulated Field to Sum the entire column in my Table and if so, how?  Will this slow things down for a large table?
0
Comment
Question by:gammill
13 Comments
 
LVL 1

Accepted Solution

by:
adeng earned 50 total points
ID: 1355566
I've made this function in my own program to sum the entire column, but i didn't uses calculated field to store the result, because it's make the process very slow and unefficient, except it's a master-detail table, i put the result in a calculated field in master table.

function TDataModule1.Total(Tabel: TDataSet; Field: tField): Extended;
var
     Total         : Extended;
     PrevRecord    : TBookmark;
begin
     PrevRecord := Tabel.GetBookmark;
     try
          Tabel.DisableControls;
          Tabel.First;
          Total := 0;
         
          while not Tabel.EOF do
          begin
               Total := Total + Field.Value;
               Tabel.Next;
          end;
          Result := Total;
         
     finally
          Tabel.EnableControls;
          if PrevRecord <> nil then
          begin
               Tabel.GoToBookmark(PrevRecord);
               Tabel.FreeBookmark(PrevRecord);
          end;
     end;
end;

{on master table on calc event }
procedure TDataModule1.Table1.CalcFields(DataSet: TDataSet);
begin
     Table1TOTAL.Value:= Total(Table2, Table2TOTAL));
end;

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1355567
hi gammil,

it can be also done with a query, like

select sum(YourFieldname) as Total from Yourtable

after query.open

you can say

YourTotalContentField := Query1.FieldbyName('Total').AsFloat;

in Code

query1.SQL.Clear;
query1.SQL.Add('select sum(YourFieldname) as Total from Yourtable');
query1.Open;
YourTotalContentField := Query1.FieldbyName('Total').AsFloat;
query1.Close;

The perfomance depends on the Database in background. SQL-Databases like ORACLE are very fast, PARADOX is slower in this case.

meikl
0
 

Author Comment

by:gammill
ID: 1355568
Thanks for the help adeng!  I'll give it a try.
0
Technology Partners: 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!

 
LVL 1

Expert Comment

by:merry_prince
ID: 8149835
Hello kretzschmar and adeng,

Can you give me a sample about OnCalcFields event? How to show detailed content of ftMeme field by it? (Please note, not create new column. but use original column)

Please advise. It's urgent. Thanks a lot.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8149911
merry,

in the oncalcevent you can't use
directly a blob-field from the original dataset

but you could attach a calculated string- or memofield,
and fill it with a query in the onCalcField-event

meikl ;-)
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8149965
kretzschmar,

Can you give a sample? Thks.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8150037
a sample,
using the table custoly in the alias dbdemos

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DBTables, DB, Grids, DBGrids;

type
  TForm1 = class(TForm)
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Table1CustNo: TAutoIncField;
    Table1Last_Name: TStringField;
    Table1First_Name: TStringField;
    Table1VIP_Status: TStringField;
    Table1Address1: TStringField;
    Table1Address2: TStringField;
    Table1City: TStringField;
    Table1StateProv: TStringField;
    Table1Post_Code: TStringField;
    Table1Country: TStringField;
    Table1Phone: TStringField;
    Table1Fax: TStringField;
    Table1EMail: TStringField;
    Table1Remarks: TMemoField;
    Table1AMemoAsCalcField: TStringField;
    Query1: TQuery;
    procedure Table1CalcFields(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Table1CalcFields(DataSet: TDataSet);
begin
  query1.Close;
  query1.ParamByName('ID').AsInteger := Table1.FieldByName('CustNo').AsInteger;
  query1.Open;
  Table1.FieldByName('AMemoAsCalcField').AsString :=
    TBlobField(query1.FieldByName('Remarks')).AsString;
  query1.Close;
end;

end.

meikl ;-)
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8156081
Hi meikl,

Thanks for your helps. If it's convenient for you. Can you provide the method "DrawCell" to show memo field?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8156943
? what does you mean ?
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8157099
I want to use OnDrawDataCell event of DBGrid to show memo field. I had know how to do it. Thanks a lot. If you would like to know the details. Please inform me.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8157393
well,

applied to the sample above
(property defaultdraw of the dbgrid must be set to false)

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
begin
  if field.FieldName = 'Remarks' then
  begin
    //show the Memo content in red
    DBGrid1.Canvas.Font.Color := clRed;
    DBGrid1.Canvas.TextRect(rect,rect.left + 2,rect.Top+2,Table1Remarks.AsString);
  end else
    DBGrid1.DefaultDrawDataCell(Rect, Field, State);
end;

hope that is it

meikl ;-)
0
 
LVL 1

Expert Comment

by:merry_prince
ID: 8157419
meikl,

Ho, you are excellent. Keep in touch. Thank you very much.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8157495
well, glad that it works for you ;-)
0

Featured Post

Technology Partners: 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

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…
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…
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…

730 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