Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Detail count in a calculated field

Posted on 2004-09-10
10
Medium Priority
?
248 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

688 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