Page Index Toggle Pages: [1] 2  Send Topic Send Topic Print Print
Hot Topic (More than 10 Replies) Sum of a field from all records (Read 2594 times)
stevec
Member
*
Offline


No personal text

Posts: 2
Joined: Jan 27th, 2004
Sum of a field from all records
Jan 27th, 2004 at 1:13am
Print Post Print Post  
1st day with Sesame... and I am having problems.
I would like to get the sum of one field from all the records in a database.  Is this something simple that I am overlooking or is it a complicated process?

Please help......  ???
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Sum of a field from all records
Reply #1 - Jan 27th, 2004 at 1:37am
Print Post Print Post  
What are you trying do? Do you want to have this figure when you Mass Update or while you add record? Both the ways it could be done.
« Last Edit: Jan 30th, 2004 at 11:19pm by Bharat_Naik »  
Back to top
 
IP Logged
 
stevec
Member
*
Offline


No personal text

Posts: 2
Joined: Jan 27th, 2004
Re: Sum of a field from all records
Reply #2 - Jan 27th, 2004 at 1:46am
Print Post Print Post  
I need it to update as records are added.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Sum of a field from all records
Reply #3 - Jan 27th, 2004 at 2:45am
Print Post Print Post  
First of all you need to put a counter field. You can use on Entry Form event 

If @IsNew and Counter ="" then
               {
                       counter = @number
                }

  Say you have four items on a form:

Item1
Item2
Item3
Total = Item1 + Item2 + Item3

Add one more element:

GrandTotal:

All of them formatted either Money or Number whatever you need to have.

I take it that you had item1.....Total  elements already there and now you added GrandTotal and Counter elements.

Select all records and sort it as you need to ( I am sure you have other fields on the form also, if not does not matter)

In Mass Update Spec:
Counter = @number
GrandTotal = Total + @xlookupr (@filename, counter-1, "counter", "GrandTotal")

This will give value to all the fields in your database in the existing records.

Now you want to set it up so, everytime you add a record and as you enter the last item, that is item3, you want to see the Total and GrandTotal

Following code goes in Item3 on ElementExit event:

Total = item1 + item2 + item3

GrandTotal = item4 + @xlookupr (@filename, 999999999, "counter", "GrandTotal")

/*this statement will lookup value GrandTotal in the next highest value of counter (Next less that 999999999, that will be the highest number in the existing records
*/

I believe this should work.
  
Back to top
 
IP Logged
 
Mathew Verghese
Member
*
Offline


Always willing to learn

Posts: 15
Location: Kuwait
Joined: Jan 12th, 2004
Re: Sum of a field from all records
Reply #4 - Jan 27th, 2004 at 4:59pm
Print Post Print Post  
Counter = @number
GrandTotal = Total + @xlookupr (@filename, counter-1, "counter", "GrandTotal")
In the above example what is this "@filename".  Do we have to substitute it with something else or just as it is.
Kindly explain.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Sum of a field from all records
Reply #5 - Jan 27th, 2004 at 5:36pm
Print Post Print Post  
If you are working with the same application and you are looking up value in the application you are working, @filename means the same application. Suppose you are working on Customer.db, and looking value in external application named Vendor.db,  you will enter application name as "data\vendor.db".

Here it is internal lookup, lookup in the same application.
  
Back to top
 
IP Logged
 
Mathew Verghese
Member
*
Offline


Always willing to learn

Posts: 15
Location: Kuwait
Joined: Jan 12th, 2004
Re: Sum of a field from all records
Reply #6 - Jan 29th, 2004 at 10:06am
Print Post Print Post  
I tried the method(s) described to get a running total in a field called "GrandTotal".  I am repeating the question:
The fields are - counter,item1,item2,Total,GrandTotal. In the add mode item1*item2 and the result goes to Total and also to GrandTotal(This is the case with the first data entry)press F10 and we get a blank form for the 2nd entry, we enter data again the result goes to Total. On element entry to GrandTotal field what I am expecting is a grand total of the two data entry I just made.  I am not getting this.  The counter works with the increment of numbers with each data entry.Please help
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Sum of a field from all records
Reply #7 - Jan 29th, 2004 at 12:12pm
Print Post Print Post  
Mathewv,

The programming suggested above is works well. I have made a test application that I will send it to you if you request so.

Threre are a couple of small issues that I am reporting today.

1. The Programming in the last element does not execute even after using a tab key. I realize that it has no where to go.  But then you put goto command and it still would not go anywhere.  The only way to execute the command is to use mouse to move out of that field. I believe, the tab key should execute the element exit event command and goto command should also be executed in the last element of the database.

2. When you xloolup money field, it reads and gets value as text. You have to convert back into money to carry out further calculation. May be it is supposed to work that way or is it a bug?

  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Sum of a field from all records
Reply #8 - Jan 29th, 2004 at 1:25pm
Print Post Print Post  
Quote:
1. The Programming in the last element does not execute even after using a tab key. I realize that it has no where to go.  But then you put goto command and it still would not go anywhere.  The only way to execute the command is to use mouse to move out of that field. I believe, the tab key should execute the element exit event command and goto command should also be executed in the last element of the database.


Up arrow, Left Arrow, and Shift-Tab, will all move backwards out of a LE.

Quote:
2. When you xloolup money field, it reads and gets value as text. You have to convert back into money to carry out further calculation. May be it is supposed to work that way or is it a bug?


Sesame is "strong-typed", so yes - you should convert numeric values before operating on them.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Mathew Verghese
Member
*
Offline


Always willing to learn

Posts: 15
Location: Kuwait
Joined: Jan 12th, 2004
Re: Sum of a field from all records
Reply #9 - Jan 30th, 2004 at 7:42am
Print Post Print Post  
Bharat,
It will be of great help if a small example file is made and sent to me.
Regarding"it has no where to go", I think if another field is made just for the cursor to move when tab is pressed, the grantotal field can function as programmed.
Thank you for the help.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Sum of a field from all records
Reply #10 - Jan 30th, 2004 at 11:27am
Print Post Print Post  
Yes, I precisely did so (added an inconspicuous element without label) cursor can land there.
  
Back to top
 
IP Logged
 
FSGROUP
Full Member
***
Offline


No personal text

Posts: 179
Location: Edmonton, Alberta, Canada
Joined: Jan 14th, 2004
Re: Sum of a field from all records
Reply #11 - Jan 30th, 2004 at 9:23pm
Print Post Print Post  
does anyone know how to calculate a total of "Amount" fields for all records with the same ID?

  
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Sum of a field from all records
Reply #12 - Jan 30th, 2004 at 9:50pm
Print Post Print Post  
Do you need to have running total while you add the record or just want to know the value that can be viewed by Mass update.
  
Back to top
 
IP Logged
 
FSGROUP
Full Member
***
Offline


No personal text

Posts: 179
Location: Edmonton, Alberta, Canada
Joined: Jan 14th, 2004
Re: Sum of a field from all records
Reply #13 - Jan 30th, 2004 at 10:50pm
Print Post Print Post  
I would like to calculate the total upon adding/updating a record.  The catch is that the Total field is on a Main form and the Amount field is on a subform.  I want to calculate this total and display it in Main once a change has been been to the Amount field on the subform. 

Why is Total in Main and not in the subform? B/c I don't see why I need a total field in the subform database. I just want to add up all the Amount field values for a client with a particular ID.

If I am not explaining myself well enough, here are my DBs:

Main:
Client ID, Name, Address, etc

Subform:
Client ID, Policy No, Amount

This is such b/c I can have many Policies and their corresponding amounts for one client.  Now, the question is, how to calculate all the amounts in the Subform for a particular client.

Sorry for the wordiness
  
Back to top
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Sum of a field from all records
Reply #14 - Jan 30th, 2004 at 11:13pm
Print Post Print Post  
What you want is given on page 152 of Programming guide under @FormResultSetTotal (form)

It is exactly what you are looking for, I think.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: [1] 2 
Send Topic Send Topic Print Print