Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Detail count in a calculated field

Posted on 2004-09-10
10
Medium Priority
?
251 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

972 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