Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem on adding data

Status
Not open for further replies.

gutay008

Programmer
Nov 9, 2015
34
PH
Good day!

I have a simple problem here, i want to add all numeric data on a single field. I already search for the sum, average function on the help of vfp9 but i dont know how to work with it. i think it only be used in sql statement.
 
Update: i think the sum function work with my problem. I have the ff code: SUM all(field) TO textbox


thank you
 
Update: i think the sum function work with my problem. I have the ff code: SUM all(field) TO textbox


thank you
 
SUM all(field) TO textbox

That is not valid VFP syntax.

If you want to calculate the total value of a given field in a table, you would do it like this:

[tt]SUM TheField TO lnTotal[/tt]

where TheField is the name of the field you want to total, and lnTotal is a variable that will hold the result.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
SUM has a scope clause, which can be ALL. Since you can leave out spaces the syntax [tt]SUM ALL(field) TO textbox[/tt] actually works, but not as you think.

First, it'll put the sum into a variable called textbox, not into a textbox. A textbox is an objecct and can't be set to some value, any object, whether a control or anything else, is created and can be released, but never can be set. The objects properties can be set. You can SUM to a numeric object property, eg [tt]SUM ALL field TO thisform.text1.value[/tt].

You see, ALL is not a function, and your field is not a parameter, ALL is a scope.
help said:
Scope
Specifies a range of records to include in the total...
...The default scope for SUM is ALL records.
That means, if you don't specify a scope, ALL records are summed anyway.

If using SUM in sql you have parenthesis like [tt]SUM(field)[/tt], but not with the SUM command.
Per example code the SUM command can even do several sums, as in SUM field1,field2 TO variable1,variable2. In SQL you'd need SUM(field1) AS sumfield1, SUM(field2) AS sumfield2.

Besides all that [tt]SUM field TO thisform.text1.value[/tt] would be ok, if done within the form code, but might still not be sufficient for your needs, ie it doesn't update automatically when data changes. If you want a sum display on your form you better set the controlsource of the sum textbox to a function returning the sum, or better yet to a form method. Then any refresh of the form or control will recompute and show the sum. A sum only is static, if the data is static, therefore setting a control.value just once will only be sufficient in that case.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top