Solved

Detail count in a calculated field

Posted on 2004-09-10
10
222 Views
Last Modified: 2010-04-05
I have two datasets bonded in a Master-Detail relationship. Is there any way to display the Detail's record count as a calculated field in the master dataset?

I already tried the following code on the master's oncalcfields event:

    MasterTblDetailSumaryField.value := DetailTbl.RecordCount;

but it gives me the total record count of the detail dataset in all the records.

Any help would be appreciated. Thank You
0
Comment
Question by:Dissaster
  • 7
  • 3
10 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12033754
unit Unit1_Q_21126996;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, Buttons, ComCtrls,
  ExtCtrls, JPEG, DBCtrls, ExtDlgs, Clipbrd;

type
  TForm1 = class(TForm)
    dstMTV: TDataSource;
    DBGridMTV: TDBGrid;
    tblMTV: TTable;
    QueryCount: TQuery;
    DBGridPHOTO: TDBGrid;
    tblPHOTO: TTable;
    dstPHOTO: TDataSource;
    tblMTVID: TAutoIncField;
    tblMTVNAME: TStringField;
    tblMTVAGE: TSmallintField;
    tblMTVCOUNT_PHOTOS: TIntegerField;
    DBImage: TDBImage;
    DBNavigator1: TDBNavigator;
    procedure FormCreate(Sender: TObject);
    procedure tblMTVCalcFields(DataSet: TDataSet);
  private   { Private declarations }
  public    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  tblMTV.Active := True;
  tblMTV.FieldByName('ID').Visible := False;
  tblPHOTO.Active := True;
  tblPHOTO.FieldByName('ID').Visible := False;
  tblPHOTO.FieldByName('MTV_ID').Visible := False;
  tblPHOTO.FieldByName('PHOTO').Visible := False;
end;

procedure TForm1.tblMTVCalcFields(DataSet: TDataSet);
var
  Count: Integer;
begin
  Count := 0;
  try
    QueryCount.Active := False;
    QueryCount.SQL.Text :=''
                        + ' SELECT COUNT(*) FROM PHOTO.DB'
                        + ' WHERE MTV_ID='
                        + tblMTV.FieldByName('ID').AsString
                        + '';
    QueryCount.Active := True;
    Count := QueryCount.Fields[0].AsInteger;
  finally
    tblMTV.FieldByName('COUNT_PHOTOS').AsInteger := Count;
  end;

end;

end.
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12033758
download above example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21126996.zip
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12034687
Under some Databases it would be:

procedure TForm1.tblMTVCalcFields(DataSet: TDataSet);
var
  Count: Integer;
begin
  Count := 0;
  try
    QueryCount.Active := False;
    QueryCount.SQL.Text :=''
                        + ' SELECT COUNT(*) FROM PHOTO.DB'
                        + ' WHERE MTV_ID='
                        + ''''
                        + tblMTV.FieldByName('ID').AsString
                        + ''''
                        + '';
    QueryCount.Active := True;
    Count := QueryCount.Fields[0].AsInteger;
  finally
    tblMTV.FieldByName('COUNT_PHOTOS').AsInteger := Count;
  end;
end;
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:Dissaster
ID: 12037321
ok..  must shamefully admit that it didn't ocurred to me that a sql might solve my problem, but woldn't it become a resource hog to use that on large detail tables? retrieving from the DB all the detail records every time the field is calculted?
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12037371
Don't worry about you don't guess for using SQL ....
You are right, it would be provoked some difficulties on a large master and detail tables using calculated fields....
I'll try to avoid that, please wait ....
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 250 total points
ID: 12037399
The solution is using a JOIN into the master Query (instead of table). This way we avoid Calculated field:

 SELECT m.ID, m.NAME, m.AGE, COUNT(p.ID) AS COUNT_PHOTOS
 FROM MTV m
 JOIN PHOTO p ON p.MTV_ID = m.ID
 GROUP BY m.ID, m.NAME, m.AGE

I will upload the example after a few minutes to the above link
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12037413
O.k., I did upload it
0
 
LVL 1

Author Comment

by:Dissaster
ID: 12049711
It worked fine, just with a slight modification:

SELECT m.ID, m.NAME, m.AGE, COUNT(p.ID) AS COUNT_PHOTOS
FROM MTV m
LEFT JOIN PHOTO p ON p.MTV_ID = m.ID
GROUP BY m.ID, m.NAME, m.AGE

With the simple JOIN i couldn't saw themaster records that didn't had detail records.. it was solved using LEFT JOIN
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 12051502
O.k. it may be depends on what kind of database is in use. I tested it with Paradox. Your database is probably different ....
Well, I am glad that it works fine after you correct the code !
Emil
0
 
LVL 1

Author Comment

by:Dissaster
ID: 12085819
Yes, i guess it depends on the database i used, in this case it was MySql
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 76
Delphi XE10 Round Image 2 129
Delphi Firemonkey Need Sample for Online Shopping Example. 2 145
Convert GUI app into console app for Win32 Env 5 101
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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