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

Quickly determine number of detail records and add to calculated field

Posted on 2004-08-10
Last Modified: 2010-04-05
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
Question by:Delphiwizard
  • 6
  • 4
  • 3
LVL 17

Accepted Solution

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.

Assisted Solution

by:Greg Rowland
Greg Rowland earned 200 total points
ID: 11765485
procedure TForm1.wwClientDataSet2AfterScroll(DataSet: TDataSet);
// 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);



Author Comment

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?

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.


Author Comment

ID: 11771480
And what do you mean by adding a where or else in the code?
Can you give me an example?
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.

Author Comment

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.

Expert Comment

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

Author Comment

ID: 11773497
Thanks for your effort to both of you.
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.

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;

Author Comment

ID: 11774202
What does a DataSetProvider and ClientDataSet do?
I've never used that.

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,


Author Comment

ID: 11774523
Thanks for the explanation. Stef

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mydac connection data base issue 3 166
Process Javascript errors with Delphi TWebBrowser 1 176
CheckListBox usage 3 71
RESTRequest Parameter 4 43
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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 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