BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

OnCalcField issue

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (dbExpress)
View previous topic :: View next topic  
Author Message
Tim
Guest





PostPosted: Mon Sep 04, 2006 4:38 pm    Post subject: OnCalcField issue Reply with quote



I have a master/detail form showing a list of detail items displayed in a
DBGrid and need to show a balance column, depending upon the value of an
'in' and an 'out' field, also shown in the grid. Because of the variable
sort order of the grid, I cannot maintain a balance field in the database,
calculated when an 'in' or an 'out' is initially entered, so it must be done
when the grid is displayed.

I have added a global variable, which is set to 0 when the master
ClientDataSet is scrolled, added a new field in the detail ClientDataSet and
then used the OnCalcFields method to generate the balance field value. (I
could do it all with SQL, but there are a couple of checks that need to be
done that make it easier to do programmatically).

In theory this should be straightforward, but it depends upon OnCalcFields
being fired once for each row of data, but it is actually fired 3 or 4 times
for each row, making the balance nonsense.

Could anyone suggest a better way of doing this?

Thanks.
Back to top
Craig Stuntz [TeamB]
Guest





PostPosted: Tue Sep 05, 2006 6:41 pm    Post subject: Re: OnCalcField issue Reply with quote



Your requirements aren't clear from your message, but have you
investigated the aggregate field features of TClientDataSet?

--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
IB 6 versions prior to 6.0.1.6 are pre-release and may corrupt
your DBs! Open Edition users, get 6.0.1.6 from http://mers.com
Back to top
Tim
Guest





PostPosted: Wed Sep 06, 2006 7:54 pm    Post subject: Re: OnCalcField issue Reply with quote



Craig,

I suppose that my question comes down to this: when is the OnCalcFields
method fired? I assumed that when a DBGrid was being populated, for each row
the method is fired once for any one particular calculated field. However,
if in populating a DBGrid it might be fired more than once, then I need to
look elsewhere for solving my problem.

To answer your question, no I have never looked at Aggregates, so I will
check it out and if it helps, then I appreciate the idea.

Thanks.

"Craig Stuntz [TeamB]" <craig_stuntz (AT) nospam (DOT) please [a.k.a. acm.org]> wrote
in message news:44fd7e89$1 (AT) newsgroups (DOT) borland.com...
Quote:
Your requirements aren't clear from your message, but have you
investigated the aggregate field features of TClientDataSet?
Back to top
Craig Stuntz [TeamB]
Guest





PostPosted: Wed Sep 06, 2006 8:20 pm    Post subject: Re: OnCalcField issue Reply with quote

Tim wrote:

Quote:
I suppose that my question comes down to this: when is the
OnCalcFields method fired?

A lot.

At the very least, it's whenever any field in the row changes and when
the row is first displayed. But there are probably more times I don't
know off the top of my head.

For this reason, any code in an OnCalcFields handler needs to be very
lightweight and in general designed to run multiple times.

--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
Want to help make Delphi and InterBase better? Use QC!
http://qc.borland.com -- Vote for important issues
Back to top
Tim
Guest





PostPosted: Wed Sep 06, 2006 9:29 pm    Post subject: Re: OnCalcField issue Reply with quote

Thanks Craig. I am not sure that Aggregates will quite do what I have in
mind, but I think I can get round the firing problem, although I will bear
in mind your comment about 'For this reason, any code in an OnCalcFields
handler needs to be very
lightweight and in general designed to run multiple times.'

Tim

"Craig Stuntz [TeamB]" <craig_stuntz (AT) nospam (DOT) please [a.k.a. acm.org]> wrote
in message news:44fee737$1 (AT) newsgroups (DOT) borland.com...
Quote:
Tim wrote:

I suppose that my question comes down to this: when is the
OnCalcFields method fired?

A lot.
Back to top
Craig Stuntz [TeamB]
Guest





PostPosted: Wed Sep 06, 2006 10:03 pm    Post subject: Re: OnCalcField issue Reply with quote

You could also try an InternalCalc field, which is re-evaluated for a
row only when the row is posted.

--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
Want to help make Delphi and InterBase better? Use QC!
http://qc.borland.com -- Vote for important issues
Back to top
Tim
Guest





PostPosted: Thu Sep 07, 2006 2:33 pm    Post subject: Re: OnCalcField issue Reply with quote

Craig,

I have been playing around a bit and getting nowhere, so if you have the
patience, I realise that my initial post was misleading.

The balance field I was referring to should have been described as a
'running balance' field. An example would be:

Invoice Credit Debit Running Balance
1 1 1
2 1 0
3 1 1
4 1 2

Obviously a 'balance' field as I initially described it would be simple, but
a 'running balance' needs to know what the previous record's running balance
was and if one record changes, then all the records after it will be
effected.

I don't think that it can easily be done, if at all, using calculated
fields. However, adding a new field to the database called running_balance,
iterating through the dataset, calculating the running balance as I go and
then displaying this field as a column in the grid does work. Not as quick
or as neat as I was hoping, but it works.

Tim
Back to top
Craig Stuntz [TeamB]
Guest





PostPosted: Thu Sep 07, 2006 5:51 pm    Post subject: Re: OnCalcField issue Reply with quote

Tim wrote:

Quote:
I don't think that it can easily be done, if at all, using calculated
fields. However, adding a new field to the database called
running_balance, iterating through the dataset, calculating the
running balance as I go and then displaying this field as a column in
the grid does work. Not as quick or as neat as I was hoping, but it
works.

Since your value by definition will change with the order of the
dataset, I think that's really all you *can* do. Furthermore, you must
recalculate them whenever the order changes. And you have to be on the
lookout for components like the Developer Express grids which can
display things in a different order than the dataset order.

--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
How to ask questions the smart way:
http://www.catb.org/~esr/faqs/smart-questions.html
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (dbExpress) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.