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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum the values from the row Percentage

Status
Not open for further replies.

Filip Brnic

Programmer
Dec 25, 2023
43
RS
Hello, i need help, here is what im trying to achieve
I have a text7 in my form, when the form starts there is a table akcionari
in that table there is a field procenat.
i want the text7.value to be 100- the sum of all records in akcionari.procenat
so if i have a record 12 in procenat i want the text7.value to be 88,
furthermore i don't know where to put this code, because in the form itself you can save records and modify them so akcionari.procenat could change
so if i change it in the form from 12 to 11, i want it to be 89 then, so basically when i change something i want that to change as well, i hope i've been clear, if someone doesnt get something ill respond.

And yes im asking for a line of code, im kinda stuck on this one and didn't find much help online even though this seems so simple :D
 
You seem to be contradicting yourself. You say that you want text7.value to be 100- the sum of all records in akcionari.procenat. But then you say you want it to be 88 (100 - 12) if you "have a record 12". What does this mean? the 12th record in the table? Or any record containing a 12 in that field?

In general, to get the sum of a certain field in all records in a table, you can use the SUM command:

Code:
SUM procenat TO lnSum
thisform.text7.Value = lnSum

But you need to be aware that that will move the record pointer to the end of the table.

Alternatively, use SQL, which doesn't move the record pointer:

Code:
SELECT SUM(procenat) FROM akcionari INTO ARRAY laSum
thisform.text7.Value = laSum(1)

You will need to do that at whatever point in the program you change the value of procent.

But I suspect this is not what you really want. If so, you need to explain the problem more clearly.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
hey chris this helped, my bad for the misunderstanding, i meant if lets say record 1 contained 12 it would be 88. regardless thank you i got it.
 
Filip Brnic said:
hey chris this helped

I haven't said a word. Do you see Mikes post as posted by me?

What is true is that I recognize the same contradiction as Mike already said. You didn't actually clarified it.
If the difference from 100 only depends on the current records procenat, then the formula is clearly just 100-procenat. And you can make an expression a controlsource, too. Just always enclose an expression with brackets, when you use it as a controlsource. Also, if those brackets are mathematically unnecessary. It helps VFP to see the controlsource is not just a variable or field or property.

And then to fulfill this...
Filip Brnic said:
when i change something i want that to change as well
The moment the changed value is stored into the procenat field is in the lostfocus of the textbox that has procenat as its controlsource. So in that textbox zou can use the lostfocus event to do just this>
Code:
thisform.refresh()

Or, more directly targetted only refresh that textbox text7:
Code:
thisform.text7.refresh()

Besides, it is not just politeness but will help anyone later, even yourself, when you rename textboxes from the name they get by putting them on the form to a name that describes what they are about, like txtProcenat txtPreostaliProcenat. Then it becomes much more obvious why code in txtProcenat.LostFocus would do...
Code:
Thisform.txtPreostaliProcenat.Refresh()
..., because it is very obvious that the rest percentage depends on the percentage.

On top of all that, it makes sense you make txtPreostaliProcenat readonly, so users can only edit procenat and that rest value is computed from the entered value, as the inverse isn't done.

Chriss
 
For some reason on my pc it says chris answered hahaha, thats strange, on my phone i see it was mikes answer now, regardless both of you helped me understand and i did what i wanted, unfortunately you know that im always explaining bad, but even trough that i still get some help from you, so thank you so much...
 
Filip Brnic said:
For some reason on my pc it says chris answered hahaha, thats strange,

On one side it's funny, and it's not even bad, when a post of Mike shows as a post from me. In general, such misalignments should be addressed, though.

Recently I have a lot of problems with the tek-tips site, not only when it's offline, but even when a page is loaded the links on it don't react for seconds, if not even minutes. I guess the tek-tips/eng-tips site software shows its aging.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top