Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Double calculation in the procedure



Permlink Replies: 16 - Last Post: Oct 31, 2017 5:19 AM Last Post By: Rudy Velthuis (...
simo musiu

Posts: 11
Registered: 2/10/03
Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2017 1:28 AM
Double calculation in the procedure
Hi, I'm trying to create a calculation, I can not find a solution.
By clicking on the button the value is correct, clicking the button again calculates the value by doubling the value, clicking again triple, and so on. I would like the value to be calculated once.

procedure Tdett_form.calcolaGiacenza;
var
scaricoTotale:extended;
begin
try
 Table1.DisableControls;
  scaricoTotale:=0;
  Table1.First;
    while notTable1.EOF do
    begin
      scaricototale:=scaricototale+Table1Scarico.Value; 
      Table1.Next;
    end;
    GiacenzaScarico.Edit;
     Giacenzascarico.Value:=Giacenzascarico.Value+scaricototale;
     Giacenza.Post;
     end;
 
finally
  Table1.enableControls;
end;
     end;


The calculation that comes out and corrected -
But clicking again recalculates again calculates everything by going to add the total again.
You should stop at just one calculation.
Thank you Simo
Roy Lambert

Posts: 1,063
Registered: 8/7/01
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2017 5:50 AM   in response to: simo musiu in response to: simo musiu
simo

Elementary logic

{code}procedure Tdett_form.calcolaGiacenza;
var
scaricoTotale:extended;
begin
try
GiacenzaScarico.Edit;
Giacenzascarico.Value:=0;
Giacenza.Post;

Table1.DisableControls;
scaricoTotale:=0;
Table1.First;
while notTable1.EOF do
begin
scaricototale:=scaricototale+Table1Scarico.Value;
Table1.Next;
end;
GiacenzaScarico.Edit;
Giacenzascarico.Value:=Giacenzascarico.Value+scaricototale;
Giacenza.Post;
end;

finally
Table1.enableControls;
end;

Roy Lambert

simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2017 6:52 AM   in response to: Roy Lambert in response to: Roy Lambert
[img]https://preview.ibb.co/btYpqm/procedure.gif[/img]

https://ibb.co/nLGW36

thank you
but it does not work .
It was so easy. There is a mastersource.
Every change should update Giacenzascarico.Value with the value.
Look at the photo .

Roy Lambert wrote:
simo

Elementary logic

{code}procedure Tdett_form.calcolaGiacenza;
var
scaricoTotale:extended;
begin
try
GiacenzaScarico.Edit;
Giacenzascarico.Value:=0;
Giacenza.Post;

Table1.DisableControls;
scaricoTotale:=0;
Table1.First;
while notTable1.EOF do
begin
scaricototale:=scaricototale+Table1Scarico.Value;
Table1.Next;
end;
GiacenzaScarico.Edit;
Giacenzascarico.Value:=Giacenzascarico.Value+scaricototale;
Giacenza.Post;
end;

finally
Table1.enableControls;
end;

Roy Lambert

simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2017 6:56 AM   in response to: simo musiu in response to: simo musiu
Giacencascarico.Value and the warehouse.
Then it updates continually.
I can not carry Giacenzascarico.Value = 0,
I reset the warehouse and update me only with the current record.
If a record change writes to that record and loses the previous one.
I hope I have been clear in my explanation. I'm italian
Thank you so much


simo musiu wrote:
[img]https://preview.ibb.co/btYpqm/procedure.gif[/img]

https://ibb.co/nLGW36

thank you
but it does not work .
It was so easy. There is a mastersource.
Every change should update Giacenzascarico.Value with the value.
Look at the photo .

Roy Lambert wrote:
simo

Elementary logic

{code}procedure Tdett_form.calcolaGiacenza;
var
scaricoTotale:extended;
begin
try
GiacenzaScarico.Edit;
Giacenzascarico.Value:=0;
Giacenza.Post;

Table1.DisableControls;
scaricoTotale:=0;
Table1.First;
while notTable1.EOF do
begin
scaricototale:=scaricototale+Table1Scarico.Value;
Table1.Next;
end;
GiacenzaScarico.Edit;
Giacenzascarico.Value:=Giacenzascarico.Value+scaricototale;
Giacenza.Post;
end;

finally
Table1.enableControls;
end;

Roy Lambert

simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2017 9:17 AM   in response to: simo musiu in response to: simo musiu
Hey Roy Lambert did you see the answer?
Thanks so much
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2017 12:23 AM   in response to: simo musiu in response to: simo musiu
simo musiu wrote:
Double calculation in the procedure
Hi, I'm trying to create a calculation, I can not find a solution.
By clicking on the button the value is correct, clicking the button again calculates the value by doubling the value, clicking again triple, and so on. I would like the value to be calculated once.

procedure Tdett_form.calcolaGiacenza;
var
scaricoTotale:extended;
begin
try
 Table1.DisableControls;
  scaricoTotale:=0;
  Table1.First;
    while notTable1.EOF do
    begin
      scaricototale:=scaricototale+Table1Scarico.Value; 
      Table1.Next;
    end;
    GiacenzaScarico.Edit;
     Giacenzascarico.Value:=Giacenzascarico.Value+scaricototale;</div>
 
This is why you are seeing a doubling, tripling etc.  You are adding your new 
value to the existing value.  So assuming the initial value is NULL the first 
time it will be
 
scaricototale
 
second time you press
 
current_value (which is scaricototale) + scaricototale
 
third time
 
current_value (which is scaricototale + scaricototale) + scaricototale
 
just change that line to
 
Giacenzascarico.Value:= scaricototale;
 
Or better yet when you need the value just sum it from the table and not try and 
store a calculated value.
 
Or if you need to store it in the master write triggers on Table1 that on 
insert/update/delete maintains the value in the master table.
 
 
 
<div class="jive-quote">     Giacenza.Post;
     end;
 
finally
  Table1.enableControls;
end;
     end;


The calculation that comes out and corrected -
But clicking again recalculates again calculates everything by going to add the total again.
You should stop at just one calculation.
Thank you Simo

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 22, 2017 1:35 AM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Thank you so much for your answer, but I did not understand it well.
You have to update your Genialcariotomotor with the new data, you have to increase it. The code you put in is fine, but if I change record I write the new data by losing the previous one.
I put you 2 pictures so you can understand better.

https://ibb.co/cEdvWR Image1
https://ibb.co/jtKPQm image2

Thankssssssss you

Jeff Overcash (TeamB) wrote:
simo musiu wrote:
Double calculation in the procedure
Hi, I'm trying to create a calculation, I can not find a solution.
By clicking on the button the value is correct, clicking the button again calculates the value by doubling the value, clicking again triple, and so on. I would like the value to be calculated once.

procedure Tdett_form.calcolaGiacenza;
var
scaricoTotale:extended;
begin
try
 Table1.DisableControls;
  scaricoTotale:=0;
  Table1.First;
    while notTable1.EOF do
    begin
      scaricototale:=scaricototale+Table1Scarico.Value; 
      Table1.Next;
    end;
    GiacenzaScarico.Edit;
     Giacenzascarico.Value:=Giacenzascarico.Value+scaricototale;</div>
 
This is why you are seeing a doubling, tripling etc.  You are adding your new 
value to the existing value.  So assuming the initial value is NULL the first 
time it will be
 
scaricototale
 
second time you press
 
current_value (which is scaricototale) + scaricototale
 
third time
 
current_value (which is scaricototale + scaricototale) + scaricototale
 
just change that line to
 
Giacenzascarico.Value:= scaricototale;
 
Or better yet when you need the value just sum it from the table and not try and 
store a calculated value.
 
Or if you need to store it in the master write triggers on Table1 that on 
insert/update/delete maintains the value in the master table.
 
 
 
<div class="jive-quote">     Giacenza.Post;
     end;
 
finally
  Table1.enableControls;
end;
     end;


The calculation that comes out and corrected -
But clicking again recalculates again calculates everything by going to add the total again.
You should stop at just one calculation.
Thank you Simo

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 24, 2017 1:58 AM   in response to: simo musiu in response to: simo musiu
Hello
no one gives me a hand?
Simo
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 24, 2017 5:23 AM   in response to: simo musiu in response to: simo musiu
simo musiu wrote:

Hello
no one gives me a hand?
Simo

No, because no one fully understands what you want, I guess. If you
want a total, you do not add to the current total, you set a new total.
If you want a maximum. only set it if the new total is higher than the
older one. Or what exactly do you want?

--
Rudy Velthuis http://www.rvelthuis.de

"Talk sense to a fool and he calls you foolish."
-- Euripides
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 24, 2017 11:31 AM   in response to: simo musiu in response to: simo musiu
simo musiu wrote:
Thank you so much for your answer, but I did not understand it well.
You have to update your Genialcariotomotor with the new data, you have to increase it. The code you put in is fine, but if I change record I write the new data by losing the previous one.
I put you 2 pictures so you can understand better.

https://ibb.co/cEdvWR Image1
https://ibb.co/jtKPQm image2

Thankssssssss you



Do not maintain running totals from the client side this will fail in multi user
situations.

Your best bet is to add triggers to the detail table and as the value you want
to keep totaled up changes update the master record by the delta change (on
insert it is 100% the new value, on update it is old value - new value, and on
delete it is 100% the old value). This way if you change the master sources
value, subsequent changes to the details column will adjust the new master.

Honestly it sounds like you have a bad data model.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2017 2:07 AM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Ok

 procedure Tdett_form.calcolaGiacenza;
var
old_val : extended;
begin
 
old_val := datamod.TTGiacenzaMangimeQtascarico.Value; // old value that must remain
 
DataMod.DettaglioP.First;
while not DataMod.DettaglioP.eof do
begin
  old_val := old_val + DataMod.DettaglioPKgMangimeSCARICO.Value;
  DataMod.DettaglioP.Next;
end;
datamod.TTGiacenzaMangime.Edit;
datamod.TTGiacenzaMangimeQtascarico.AsFloat:=old_val; // new updated value 
datamod.TTGiacenzaMangime.Post;
end; 


The problem is that by clicking again, it recalculates everything and the value doubles.
It does not just add to the changes, but it adds everything to me again.
Here's how the value multiplies infinitely at each click.
I got it ?

Thank
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2017 2:18 AM   in response to: simo musiu in response to: simo musiu
simo musiu wrote:

Ok

 procedure Tdett_form.calcolaGiacenza;
var
old_val : extended;
begin
 
old_val := datamod.TTGiacenzaMangimeQtascarico.Value; // old value
that must remain
 
DataMod.DettaglioP.First;
while not DataMod.DettaglioP.eof do
begin
  old_val := old_val + DataMod.DettaglioPKgMangimeSCARICO.Value;
  DataMod.DettaglioP.Next;
end;
datamod.TTGiacenzaMangime.Edit;
datamod.TTGiacenzaMangimeQtascarico.AsFloat:=old_val; // new updated
value datamod.TTGiacenzaMangime.Post;
end; 


The problem is that by clicking again, it recalculates everything and
the value doubles. It does not just add to the changes, but it adds
everything to me again. Here's how the value multiplies infinitely
at each click. I got it ?

 
  procedure Tdett_form.calcolaGiacenza;
  var
    val : extended;
  begin
    val := 0.0;
    DataMod.DettaglioP.First;
    while not DataMod.DettaglioP.eof do
    begin
      val := val + DataMod.DettaglioPKgMangimeSCARICO.Value;
      DataMod.DettaglioP.Next; 
    end;
    datamod.TTGiacenzaMangime.Edit; 
    datamod.TTGiacenzaMangimeQtascarico.AsFloat := val;
    datamod.TTGiacenzaMangime.Post;
end; 


As long as you add to the old value, things will become more and more
and more... Now if you still need the old value somehow, then keep it
in a separate place:

    old_value := datamod.TTGiacenzaMangimeQtascarico.Value;
    SomePlace := old_value;

--
Rudy Velthuis http://www.rvelthuis.de

"Woman was God's second mistake."
-- Friedrich Nietzsche (1844-1900)

simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 27, 2017 1:22 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
 old_value := datamod.TTGiacenzaMangimeQtascarico.Value;
    SomePlace := old_value;


where do I put it and how do I use it?
Thank you


Rudy Velthuis (TeamB, MVP) wrote:
simo musiu wrote:

Ok

 procedure Tdett_form.calcolaGiacenza;
var
old_val : extended;
begin
 
old_val := datamod.TTGiacenzaMangimeQtascarico.Value; // old value
that must remain
 
DataMod.DettaglioP.First;
while not DataMod.DettaglioP.eof do
begin
  old_val := old_val + DataMod.DettaglioPKgMangimeSCARICO.Value;
  DataMod.DettaglioP.Next;
end;
datamod.TTGiacenzaMangime.Edit;
datamod.TTGiacenzaMangimeQtascarico.AsFloat:=old_val; // new updated
value datamod.TTGiacenzaMangime.Post;
end; 


The problem is that by clicking again, it recalculates everything and
the value doubles. It does not just add to the changes, but it adds
everything to me again. Here's how the value multiplies infinitely
at each click. I got it ?

 
  procedure Tdett_form.calcolaGiacenza;
  var
    val : extended;
  begin
    val := 0.0;
    DataMod.DettaglioP.First;
    while not DataMod.DettaglioP.eof do
    begin
      val := val + DataMod.DettaglioPKgMangimeSCARICO.Value;
      DataMod.DettaglioP.Next; 
    end;
    datamod.TTGiacenzaMangime.Edit; 
    datamod.TTGiacenzaMangimeQtascarico.AsFloat := val;
    datamod.TTGiacenzaMangime.Post;
end; 


As long as you add to the old value, things will become more and more
and more... Now if you still need the old value somehow, then keep it
in a separate place:

    old_value := datamod.TTGiacenzaMangimeQtascarico.Value;
    SomePlace := old_value;

--
Rudy Velthuis http://www.rvelthuis.de

"Woman was God's second mistake."
-- Friedrich Nietzsche (1844-1900)

Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 31, 2017 5:19 AM   in response to: simo musiu in response to: simo musiu
simo musiu wrote:

 
  old_value := datamod.TTGiacenzaMangimeQtascarico.Value;
  SomePlace := old_value;


where do I put it and how do I use it?

That is your decision. I don't know why you need to keep it. Fact is
that when you modify it, it can't keep its old value at the same time.
So you need some place to store it. It is up to you where and when.
That is part of programming.

--
Rudy Velthuis http://www.rvelthuis.de

"Think my friends! You adulate a stone as Vishnu but you cannot
see God in a fellow man."
-- Sankaracharya
simo musiu

Posts: 11
Registered: 2/10/03
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 30, 2017 1:51 AM   in response to: Rudy Velthuis (... in response to: Rudy Velthuis (...
hello you read the message?
Thank you very much

Rudy Velthuis (TeamB, MVP) wrote:
simo musiu wrote:

Ok

 procedure Tdett_form.calcolaGiacenza;
var
old_val : extended;
begin
 
old_val := datamod.TTGiacenzaMangimeQtascarico.Value; // old value
that must remain
 
DataMod.DettaglioP.First;
while not DataMod.DettaglioP.eof do
begin
  old_val := old_val + DataMod.DettaglioPKgMangimeSCARICO.Value;
  DataMod.DettaglioP.Next;
end;
datamod.TTGiacenzaMangime.Edit;
datamod.TTGiacenzaMangimeQtascarico.AsFloat:=old_val; // new updated
value datamod.TTGiacenzaMangime.Post;
end; 


The problem is that by clicking again, it recalculates everything and
the value doubles. It does not just add to the changes, but it adds
everything to me again. Here's how the value multiplies infinitely
at each click. I got it ?

 
  procedure Tdett_form.calcolaGiacenza;
  var
    val : extended;
  begin
    val := 0.0;
    DataMod.DettaglioP.First;
    while not DataMod.DettaglioP.eof do
    begin
      val := val + DataMod.DettaglioPKgMangimeSCARICO.Value;
      DataMod.DettaglioP.Next; 
    end;
    datamod.TTGiacenzaMangime.Edit; 
    datamod.TTGiacenzaMangimeQtascarico.AsFloat := val;
    datamod.TTGiacenzaMangime.Post;
end; 


As long as you add to the old value, things will become more and more
and more... Now if you still need the old value somehow, then keep it
in a separate place:

    old_value := datamod.TTGiacenzaMangimeQtascarico.Value;
    SomePlace := old_value;

--
Rudy Velthuis http://www.rvelthuis.de

"Woman was God's second mistake."
-- Friedrich Nietzsche (1844-1900)

Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 31, 2017 5:17 AM   in response to: simo musiu in response to: simo musiu
simo musiu wrote:

hello you read the message?

Nope. I was away (trip to Sweden).

--
Rudy Velthuis http://www.rvelthuis.de

"Life's under no obligation to give us what we expect."
-- Margaret Mitchell

Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Double calculation in the procedure
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 26, 2017 10:14 AM   in response to: simo musiu in response to: simo musiu
simo musiu wrote:
Ok

 procedure Tdett_form.calcolaGiacenza;
var
old_val : extended;
begin
 
old_val := datamod.TTGiacenzaMangimeQtascarico.Value; // old value that must remain
 
DataMod.DettaglioP.First;
while not DataMod.DettaglioP.eof do
begin
  old_val := old_val + DataMod.DettaglioPKgMangimeSCARICO.Value;
  DataMod.DettaglioP.Next;
end;
datamod.TTGiacenzaMangime.Edit;
datamod.TTGiacenzaMangimeQtascarico.AsFloat:=old_val; // new updated value 
datamod.TTGiacenzaMangime.Post;
end; 


The problem is that by clicking again, it recalculates everything and the value doubles.
It does not just add to the changes, but it adds everything to me again.
Here's how the value multiplies infinitely at each click.
I got it ?

Thank

You can't do it this way. This will not work in a multi user situation or in
the situation where you hit the button twice.

either

a) maintain this column through triggers on the server
b) have 2 columns in your TTGiacenzaMangimeQtascarico table. The master base
value and a second column that is the rolled up sum of the details and you over
write the details every time you want to recalculate that part.
c) let your server just do the work. Only store the base value in hte master and
add to that the sum of the details right from the server in SQL.

Your method is fundamentally flawed because you can not start from the same base
value each time you click the button. You can not do it this way even throwing
out that this won't work in a multi user situation.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02