Solved

Quickly determine number of detail records and add to calculated field

Posted on 2004-08-10
13
239 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
  • 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:SurferJoe
SurferJoe 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:SurferJoe
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:SurferJoe
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:SurferJoe
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now