Solved

Quickly determine number of detail records and add to calculated field

Posted on 2004-08-10
13
250 Views
Last Modified: 2010-04-05
Hi,
I want to quickly determine the number customers and use that number at the Company level.

Now I solved it in the OnAfterScroll-event of the Company (with SQL)
When loading the company table into a grid this takes forever, because for each record the SQL must execute.

Does anybody know a fast solution for this?
Please supply some code.

Thanks Stef
0
Comment
Question by:Delphiwizard
[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
  • 6
  • 4
  • 3
13 Comments
 
LVL 17

Accepted Solution

by:
Wim ten Brink earned 300 total points
ID: 11761718
select count(*) from detail where DetailID=MasterID

The ID's will represent the link you have between master and client. And if you select the records for the master, you could include this count as part of your master-query.

Another option would be if your master-table and detail table are linked together. Then walking through the master table will update the detail table immediately. The DetailTable.Recordcount should tell you the number of records the detail table will have at that moment.

But you want count to be part of your master-query so you must add the Count(*) to your master-query. Perhaps something like:

select Master.Whatever, count(Detail.DetailID)
from Master, Detail
where Master.MasterID=Detail.DetailID

Make sure that DetailID is identifiable as a field of the detail table. I just did that by using the tablename as prefixes.
0
 
LVL 4

Assisted Solution

by:Greg Rowland
Greg Rowland earned 200 total points
ID: 11765485
procedure TForm1.wwClientDataSet2AfterScroll(DataSet: TDataSet);
begin
// After Scroll  works good, Before Scroll was resetting the Datasets Eof
// used else where in the code
  Label1.Caption :=
    'Record ' + IntToStr(TCustomADODataSet(DataSet).RecNo) +
    ' of ' + IntToStr(TCustomADODataSet(DataSet).RecordCount);

end;

0
 

Author Comment

by:Delphiwizard
ID: 11771298
Hi SurferJoe,

The way you discribe it, I will probably see the Label1.Caption displaying all different counts while loading the dataset into the grid.
For each afterscroll once.
Can that be surpressed till the dataset is fully loaded?

0
Independent Software Vendors: 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!

 

Author Comment

by:Delphiwizard
ID: 11771480
And what do you mean by adding a where or else in the code?
Can you give me an example?
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11772195
I suggested adding a where-clause. :-)

Basically, you could select the master records by using a query. That way, you could also include a count of all it's children in the master table. That way, calculating the number of children becomes just part of your master record. Quite nice if you display this in a grid, since every record will have a field telling you the number of child records.
0
 

Author Comment

by:Delphiwizard
ID: 11772298
And how do I update changing I made to the records in the grid?
I know how it works with TADOTable, but not with TADOQuery.
What properties must be set?

Building the SQL is not a problem for me.
0
 
LVL 4

Expert Comment

by:Greg Rowland
ID: 11772353
Just try it you’ll like it.
0
 

Author Comment

by:Delphiwizard
ID: 11773497
Thanks for your effort to both of you.
Stef
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11773851
In general, to make updates, just use INSERT or UPDATE queries. That would be the better option anyway if you use an SQL-based database. Besides, with ADO, queries are slightly faster than Tables anyway.

Still, ADO is pretty smart internally. It might be able to still update the data. :-) Not all ADO queries become read-only.
0
 
LVL 4

Expert Comment

by:Greg Rowland
ID: 11774146
Typically I would start with and ADOConnection component, connected to an ADOQuery, or ADOTable then a DataSetProvider connected to a ClientDataSet, finally a DataSource and DBGrid or other data aware component.  If you want to use ADO this is the best solution I have found.

See this tread for a little more;
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21083190.html
0
 

Author Comment

by:Delphiwizard
ID: 11774202
What does a DataSetProvider and ClientDataSet do?
I've never used that.
0
 
LVL 4

Expert Comment

by:Greg Rowland
ID: 11774464
These components where developed for Midas/DataSnap multi-tier applications. However the middle tier application server is not required and is left out in our implementation.
In a nutshell the ClientDataSet client side in memory capabilities significantly improves performance by limiting round trips to the server. In a SQL Server environment this also eliminates conflicts/blocking between users. The developer also has a lot more control over WHEN data is actually posted back to the server. This can be a good, sometimes not. The dataset provider is part of the whole modal with out going into to much detail.

Just make sure to limit the fields in where clause to avoid posting issues. Typically I would use ONLY the Key field in the where clause, but make sure not to use a Key/Identity field in the update clause. Also in SQL environments it is very important to trim the milliseconds off of DateTime fields. Otherwise the client thinks another user has changed the record and will report an error, if there is any delay in server response during a post. That was a real headache to figure out.

Good luck,

G
0
 

Author Comment

by:Delphiwizard
ID: 11774523
Thanks for the explanation. Stef
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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