Topic: Instant calculation

Hi Guys,


How can I do instant calculation and display calc result on same form?


For example, there is a Starting balance, Ending balance and Profit/Loss fields. What I like to do is once user enters values in starting and ending balance fields, calculation is done and result displayed in Profit/Loss field on same form. If calc result is minus figure then it's displayed with minus sign in red color.

Post's attachments

Attachment icon Instant Calc.zip 2.76 kb, 424 downloads since 2016-10-23 

Adam
God... please help me become the person my dog thinks I am.

Re: Instant calculation

Here you go. Since the Profit/Loss field is Read Only I also turned off the tab stop.

Post's attachments

Attachment icon Instant Calc (2).zip 580.62 kb, 433 downloads since 2016-10-23 

3 (edited by AD1408 2016-10-24 01:14:11)

Re: Instant calculation

Hi ehwagner,


Excellent. Thanks a lot.............


I liked the way you did; without changing field type to calculated on dbTable.


----------------------------------------


Edit:

I checked it bit more and noticed following issues:
1. When numbers typed in edit1, it shows on edit3 as a minus numbers. Since calc needs two numbers, perhaps we don't print in edit3 before entering numbers in edit2


2. When typed a minus numbers, calculations are not performed correctly. For example:
Edit1 50.00, Edit2 -100.00 produces -150.00 instead of -50.00


3. I think we need cancel := when user types minus numbers  in Edit and message box saying 'Starting balance must be bigger than zero'

Shortcomings are on my side. I should have specified all these in the first post. My apologies...

Adam
God... please help me become the person my dog thinks I am.

Re: Instant calculation

An example
http://myvisualdatabase.com/forum/viewtopic.php?id=1416

Dmitry.

Re: Instant calculation

Hi Dmitry,


Thanks a lot.....

Example is fine for multiplications but on adding and subbing I encounter issues. Perhaps my logic is rotten.
The following code works OK:


//edit1 is starting_balance
//edit2 is ending_balance
//edit3 is profit_loss (calculated field)

procedure CalculateTotal;
begin
    Form1.Edit3.Value := Form1.Edit1.value * Form1.Edit2.Value;
    If Form1.Edit3.Value <0 then Form1.Edit3.Font.Color := clRed else Form1.Edit3.Font.Color := clWindowText;
end;


procedure Form1_Edit1_OnChange (Sender: string);
begin
    CalculateTotal;
end;

procedure Form1_Edit2_OnChange (Sender: string);
begin
    CalculateTotal;
end;

However, when ending balance is minus figure then calculation is incorrect. One can start with $100.00 and ends up with -$50.00. In this situation shouldn't result show -$150.00?


Also wanted to prevent user entering a minus figure in Edit1 with following code but didn't work:

begin
    If Form1.Edit1.Value <0 then
    begin
    Cancel := True;
    ShowMessage('Starting balance value must be bigger than zero');
    end;
end;
Adam
God... please help me become the person my dog thinks I am.

Re: Instant calculation

Adam,
The calculation is correct. Edit 1 is the Starting Balance and Edit2 is the Ending Balance. If you started with 50 and ended with a -100, then you have a loss of 150. In other words you lost your original 50 and then you lost another 100 to get to a negative 100.

I updated the example with what you want to do with the firing of the calculation and the validation of the fields. I personally do not like doing it this way, but not knowing your project and what the intentions are, I made the adjustments for you.

Hope it helps.

Post's attachments

Attachment icon Instant Calc (3).zip 581.47 kb, 417 downloads since 2016-10-24 

Re: Instant calculation

Evening Adam,

Getting the calculation to fire off correctly is trickier than you first think - what sequence is the user entering the figures in, do they 'tab' or 'backtab', do they backspace out a field (leaving it empty / not null / zero).
Anyways, this is the way I think I'd do it.  Probably other ways as well.
Derek.

Post's attachments

Attachment icon instantcal djs.zip 336.63 kb, 410 downloads since 2016-10-24 

Re: Instant calculation

Hi Again Adam, EHW,
Just sent my response and then saw that EH has already replied. 
Sorry about that.
Derek.

9 (edited by AD1408 2016-10-24 22:13:17)

Re: Instant calculation

Hi ehwagner,
Hi Derek,


Thanks for the latest updates.....


Lets consider real life situation.  For instance, I can start a business with borrowed money (minus starting balance) or my own money (plus starting balance) but I cannot start with zero (this is where pop-up message should be applicable as 'Starting balance cannot be zero' in proper way of doing it. No message needed for ending balance as I can end with plus, minus or zero.


examples:
1. Starting balance 100, ending balance -50 calc profit / loss should show: -150
2. Starting balance 100, ending balance 0 calc profit / loss should show: -100
3. Starting balance -100, ending balance 25 calc profit / loss should show: -75
4. Starting balance -200, ending balance -100 calc profit / loss should show: -300
5. Starting balance 150, ending balance  175 calc profit / loss should show: 25
6. Starting balance 150, ending balance  125 calc profit / loss should show: -25


Form1.Edit3.Value := Form1.Edit2.value - Form1.Edit1.Value;

Since edit1 holds the base value shouldn't the above be like this:

Form1.Edit3.Value := Form1.Edit1.value - Form1.Edit2.Value;

On latest update, unfortunately instant display is gone. I assume it's due to waiting for user to enter ending balance value before calc takes place.

Adam
God... please help me become the person my dog thinks I am.

Re: Instant calculation

Hi Adam,
I'm a bit confused (often happens!).  In your earlier post you said you:

"also wanted to prevent user entering a minus figure in Edit1 with following code but didn't work

and your error message you wanted to display was

Starting balance value must be bigger than zero'

This is what I based the example on that I attached to you.
But in your latest post, you quote two calculation examples where the starting balances (Edit1) can be negative. 
Anyway, even if not mathematically correct, it should still enable you to see how to write the code for the situation you face.
Derek.

Re: Instant calculation

Hi Derek,


No doubt my logic on this math stuff not conventional and in standard math most likely wrong. What you did is correct according my previous posts, which I appreciate the sample you did. It'll definitely be useful for me. Thanks again.... I hope you and ehwagner can forgive my inconsistency in this matter.


With the latest post of mine, I tried to illustrate practical aspect of it, if it's doable.

Adam
God... please help me become the person my dog thinks I am.

Re: Instant calculation

Forgiveness is not necessary. It's understandable. You're designing and developing simultaneously while learning MVD. That can be challenging. I did change the approach of instant calc based on your request for calculating on entry into the Ending Balance (Edit2). As I said before I do not particularly like that approach because it can be problematic down the road depending on how you use the process in your larger project. It can be dangerous to only fire off a calculation on only one of multiple fields involved in a calculation. If a user changes data in a non-firing field, then no calculation will be performed. Anyway, enough of that. I think you get the picture. The attached is a hybrid of what I had before (and Derek too). It goes back to instant calc with a validity check on the starting balance. Very simple. MVD is pretty flexible in that you can create multiple approaches to tackle various situations.

I am curious though in what the logic is in your Starting Balance and Ending Balance calculation. Unless mathematics has changed since I've been in school, in order to get the difference between a starting balance and ending balance you must subtract the starting from the ending. In your examples 1, 2, 5, and 6 you in fact do subtract the starting from the ending. However, for 3 and 4 you add the two. Does the starting balance take on a new definition when it is negative versus positive. To me a starting balance is a starting balance regardless if it is negative or positive. Just curious.

Post's attachments

Attachment icon Instant Calc (4).zip 580.96 kb, 456 downloads since 2016-10-26 

Re: Instant calculation

Hi ehwagner.


Thanks a lot for the latest sample project.....


I was trying to apply math to live situations in terms of business but my logic may be totally flawed.


Agreed, " starting balance is a starting balance regardless if it is negative or positive" as long as it's not zero (in terms of business logic). One can start a business with with negative (borrowed money) or positive balance. If started with negative balance of 100.00 and on the way borrowed further 50.00 as cash-flow starting to get thin (where ending balance -50.00) then liability becomes -150.00.  Minus figures needs to be added and showed as minus figure. As I said my approach may be wrong, there may be better way of dealing with such situations.


Once again, thank you for your kind help and patience.

Adam
God... please help me become the person my dog thinks I am.