Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using SUM, COUNT, etc. in a form with multiple tables
Message
From
05/11/1998 02:01:18
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
04/11/1998 13:23:10
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00154405
Message ID:
00154701
Views:
19
>>>I have a form with several tables in its data environment. If I want to use SUM to sum a field in one particular table, what should I do? (How can you specify the table in SUM command?) Is there any better way to get a sum of a field in a table without worrying working area while multiple tables exist in a data environment? Thanks in advance.
>>You can use SQL to sum to an array. So won't worry about current workarea, rec pointer movement etc. ie: select sum(sumfld1), sum(fld2*fld3/3) into array aSums. Further you could set array elements as controlsources of textboxes that would show the sum (so no need for refresh too :).
>>Cetin
>
>Thank you, Cetin. But is there any better way than using SQL? The reason for asking this is that I have multiple tables in the form. When SQL runs it changes the work area which is the whole point that I am trying to avoid.
>
>I use a field to show the currect sum of some records in a table(grid) which is editable on the form. In other words, I want to have a textbox on a one-to-many form. Whenever I add/delete/change records in the grid (detail table) or I move the records in the master table, the updated sum of related records in the detail table will be displayed in the textbox.

Wei,
It's wrong SQL select into array causes workarea and/or recordpointer to change. It changes when you select into cursor (yet grid knows its own recordsource). I get your main purpose is having a sum of child records in grid. Well a fast logic (buffering on ? in this sample assumed to be on):
* thisform.txtSum is the control showing sum
*
* custom method SumMyField updating sum for parent rec change
* 
lParameters tuParKey, tcPKField, tcMyField, tcMyChild
select sum(tcMyField) ;
  from (tcMyChild) ;
  where &tcPKField = tuParKey ;
  into array aSumOf
return iif(type("aSumOf")="N",aSumOf[1],0)
*
* any button.click causing parent rec change

* default code
thisform.txtSum.value = ;
   thisform.SumMyField("ALFKI","Cust_id","order_amt","orders")
* In fact txtSum could be based on a textbox class
* having custom methods as Update_ParentChanged, 
* props LinkKeyFieldName, SumFieldName, SumTableName
* so Update_ParentChanged code would be something like :
* this.value = ;
   thisform.SumMyField( ;
   customer.cust_id, ;
   this.LinkKeyFieldName, ;
   this.SumFieldName, ;
   this.SumTableName)
* Here the important point is : There is a custom method
* doing SQL sum <b>only</b> when parent rec changes
* There is <b>no need</b> to resum as rec contents change in child sumfield
* Also if there were a lot of child recs per particular parent,
* it would definitely be a performance penalty to do sum again
* while parent rec haven't changed at all.
*
* Now up to this point we only summed a child field as parent rec
* changes and wrote the value to thisform.txtSum control
* As grid SumField content changes it should be updated to reflect the new sum
* Grid.SumField.valid - Or lostfocus as I prefer in fact
local lcFieldName,lcAliasName,lnOldValue
lcFieldName = substr(this.controlsource, ;
              rat(".",this.controlsource)+1)
lcAliasName = substr(this.controlsource, ;
              1, rat(".",this.controlsource)-1)
lnOldValue  = oldval(lcFieldName,lcAliasName)
thisform.txtSum.value = thisform.txtSum.value + ;
  (this.value - iif( isnull(lnOldValue), 0, lnOldValue) )
I hope it's clear now and helps.
Tip : Create custom textbox classes for fields to be summed in grid and for the textbox that would show the result on form.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform