Topic: One Form MVD app

I'm trying to do some basic calculators.


All on one form with the use of tab control. I like to add calc values in input part and when I click on calculate button, results are shown on output part of the same form. Please see the attached sample project.

Post's attachments

Attachment icon Working on same form.zip 5.1 kb, 436 downloads since 2016-09-11 

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

Re: One Form MVD app

Hello AD1408,


I worked a little on your project.


I am not an expert in this 'financial stuff' so I based my work on your calculated fields definitions in the database table.


The main structure of the program is there, but keep in mind that there is not data control in the script. For example, what should happen if one or more of the fields are left empty --> should the calculations be performed or should the user receive a warning ?


I commented the code so that you will find your way back through it, but it's quite simple.

1- Declare your variables with care (type)
2- Assign your variables to your edit fields
3- Code your calculations linked to the 'on click' event of the button (eventually adding controls with if /then)
4- Display the results in the correct edit fields


Have fun and do not hesitate if you have more questions


Cheers


Mathias

Post's attachments

Attachment icon Working on same form.zip 336.16 kb, 441 downloads since 2016-09-11 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

3 (edited by AD1408 2016-09-11 12:16:04)

Re: One Form MVD app

Hi Mathias,


Thanks a million....... Excellent work...........


I have simplified it further by taking out some field and commenting out code refers to deleted fields.
Also tuned in bit more of currency fields display and tab-order. Pasted a code to hide menus.


mathmathou wrote:

The main structure of the program is there, but keep in mind that there is not data control in the script. For example, what should happen if one or more of the fields are left empty --> should the calculations be performed or should the user receive a warning ?


User need to fill all required fields, otherwise produces an error dialog as it does currently. Only thing, we need to change the error dialog message from "Invalid floating point operation" to "All fields marked with an asterisk (*) are required."


Updated sample project is attached.

Post's attachments

Attachment icon Working on same form 2.zip 6.7 kb, 429 downloads since 2016-09-11 

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

4 (edited by mathmathou 2016-09-11 23:12:15)

Re: One Form MVD app

Hello AD1408,


Hooooooo, that's a tricky question !! I love that !


I adapted the code for the purpose you describe, but here is a little explanation :


What you want is make sure that the 4 edit fields ARE NOT empty.


To do that, YOU CAN NOT use a comparison with NULL or NIL or even with 0 :
- NIL means the variable if of type unknown, which is not true because you declared your variables as real
- NULL (IS NULL or = NULL) could be used, but not in your case because 0 value is acceptable
- For the same reason, you can not use 0 because in some case this value is acceptable.


The solution ?


You declared your variables as real, but they display in edit fields which contain text (even if you specified numbers only). So what we are going to test is that those edit fields are not empty. To do that, you can use :

Form1.Edit1.Text <> '';

This will make sure that the edit field is not empty, or

Length(Form1.Edit1.Text) <> 0;

which will test that the length of the content of the edit field is not 0


And if you want to be really thorough on your test (because some of your fields or not marked as Numbers Only), you could also make sure that white-spaces are taken into account, because they count as characters with the Trim command.
This command erases leading and trailing white-spaces.

Trim(Form1.Edit1.Text) <> '';

to make sure that the trimed content is not empty
or

Length(Trim(Form1.Edit1.Text)) <> 0;

to make sure that the Length of the Trimed content is not equal to 0 (trim before counting the length)


Now, unfortunatly, this is not enough in your case, because of the formula you use : there is a division in your second formula, and you can not divide by 0 (well, you can, but the NaN result does not interest you).


So there is a another serie a test to conduct before obtaining the result.


I added another variable in your code, that I called Divisor. Divisor is the result of

SLossPips * StdLotPipValue

If the result of this operation is 0, we know that the result of

(AccBalance * RiskPercent / 100) / Divisor;

will give you an error (the famous "invalid floating point operation you had previously).

The final code structure I opted for is like :

if any of the 4 edit fields is empty then
        begin
            Display a warning
        end
    else if none of the fields are empty then
        begin
            if Divisor = 0 then
                   begin
                         Display a Warning
                    end
                else if Divisor <> 0 then
                    begin
                         Perform the calculation and display the results
                    end;
        end;

Another possible approach would be to test that the two last edit fields are NOT EMPTY and NOT equal to 0, that(s up to you.


Hope this helped


Cheers


Mathias

Post's attachments

Attachment icon Working on same form 2.zip 337.7 kb, 435 downloads since 2016-09-12 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: One Form MVD app

Hello Derek,

Do you know what I like in your approach ? The minimalistic code ! That's definitely a must have.

Very nice work :-)


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: One Form MVD app

Hello Adam, Hello Mathias,
I was just having a look at your project and there are a couple of points (and if you know this already, just ignore me - ha ha!).
You need to understand that calculated fields are not actual fields that are stored in tables - have a look at screenshot 1.jpg in the attachment .  Calculated fields are just empty containers that temporarily store the result of calculations based on actual fields in the table.  This is why, in your very first attempt, the calculation was not being done - you have no 'save' button so no actual fields are being written to the table and therefore, if you don't have any actual fields written to the table, there is no data to perform the calculation on.
If you understand this, you see that with Mathias' solution (which works perfectly) you don't need a table defined at all.  It uses the edit fields from Form1 to input values, then performs calculations solely within the script and then outputs the result back to Form1.
Maybe you'll find the attachment of interest - it takes another approach and actually saves your input fields to the table and, because it then has some actual data to use, can perform the formulae defined in your calculated fields and can then display the result back to you.
It doesn't matter which approach you use but maybe seeing the two helps you to better understand the advantages and disadvantages of calculated fields against doing calculations in a script.
Derek.

Post's attachments

Attachment icon Working on same form with script.zip 462.52 kb, 438 downloads since 2016-09-12 

Re: One Form MVD app

Wooops, Derek's post has disappeared..... LOL

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: One Form MVD app

Hi Mathias
LOL! - yes, I noticed a spelling mistake so I delete and reposted!
Yes, for me 'minimalist' = EASY!! and less mistakes for me to make.
Derek.

9 (edited by AD1408 2016-09-12 13:38:27)

Re: One Form MVD app

Thanks a lot Mathias........


Thanks a lot Derek........


Mathias,
GREAT Work!!!
First I didn't notice division by zero warning message but it was there staring at me.. All good atm.


Derek,
Nice and simple approach. Unless, I'm getting things wrong, it doesn't check all fields values are bigger than zero to perform the calculation.
Feeling bit dumb for not being able to find your required fields message text to edit.


Once again, thank you so much guys...........

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

Re: One Form MVD app

Hello Adam,
If you have defined your fields as 'not null', then standard MVD will check that something has been entered into those fields - but 0 is allowed (and may well be valid).
However, what you could do in Form1 of your example, is to untick the 'currency' object property for your two currency fields (see attached screen shot) so that when you run the application, all 4 fields (2 currency, 1 real and 1 integer) display as blank;  at least then the User has to make the conscious decision to enter a value of 0 (rather than it defaulting  to 0 as it does when the 'currency' property is left ticked).  It is perhaps a bit less rigorous than the error checking that Mathias suggested to you but it can be done quickly and with no scripting.
To my knowledge, the message "This field is required......" that you get when a field has been left blank, is a standard message that cannot be amended.  It is 'switched on' when you configure the 'save' button on your forms.
Derek.

Post's attachments

Attachment icon 2.jpg 169.71 kb, 344 downloads since 2016-09-12 

Re: One Form MVD app

Thanks a lot Derek...................


Another useful and simple approach from you. Good stuff.


I'm collecting all the valuable info I get from you guys in a RTF editor with my understanding notes under the name "Rough guide to MVD" Once I get enough and useful info I'll share it here. It may help other starter users of MVD.


I haven't looked at Sqlite browser yet. Is there a topic/s about it, explaining how and where we use it?

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

Re: One Form MVD app

Hi Mathias,


I have tried to add other calculators with your code guidance but couldn't get it work. Please see, if you can get around to make them work. Btw, Your commenting on code is great. They help me a lot.

Post's attachments

Attachment icon Working on same form 3.zip 10.72 kb, 448 downloads since 2016-09-13 

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

Re: One Form MVD app

Hello Adam,


That project of yours is really growing fast !! That's a nice calculator you have there.


OK, I had a look at your code and you have two very small flaws in your code.


First of, (and the error message is quite explicit about it), you forgot a bunch of

 ; 

at the end of your instruction lines.

Every line of code must end with that sign.


A few exceptions though :

- when you do a loop for example like

for i := 0 to 100 do
     begin

     end;

there is no stop sign at the do command
-when you chain conditions like :

begin {1}
    if i > 0 then
        begin{2}
            //Some awesome code
        end{2}
    else if i = 0 then
        begin{2}
            //more awesome code
        end{2}
    else if i < 0 then
        begin{2}
            if (i < 0)  AND (i <= -10) then
                begin{3}
                    //even more awesome code
                end{3}
            else if i < 11 then
                begin{3}
                    //the summum of your code
                end;{3}
        end;{2}
end;{1}

Look at that "stair like" indentation, and notice only the last end instructions take a stop sign


When I nest conditional instructions, I like to add a 'level number' after each to keep track of the level I'm in (this is not mandatory, this is just a help), but more than 4 or 5 levels become difficult to read, and there are code alternatives to handle such things.


Finally when you will have added all the stop signs that are missing, you will get a last error message saying

Identifier redeclared: 'Form1_Button5_OnClick' at 170:32


This means you have declared twice the same procedure. Look a up in your code, that button click action is already declared. All you need to do is add the new actions to the initial procedure.


Try it for yourself, and if you do not succeed, I'll send the corrected code.


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

14 (edited by AD1408 2016-09-14 12:15:34)

Re: One Form MVD app

Hi Mathias,


Thank you so much for the info above. I love your instructions. You are a great teacher.


I got pivots calc compiled.


Working on CAGR calc... Once finished I'll upload it for your inspection - hopefully without bugs.

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

15 (edited by AD1408 2016-09-15 12:07:18)

Re: One Form MVD app

Hi Mathias,


I got it compiling  all 3 calcs without errors. However, transforming math formulas into code formulas is difficult at present for me.


Could you please check and correct them if possible?
Pivot calc's math formulas put in the code as comments in Pivot section of the code.
Implementation of CAGR calc formula by me is definitely wrong. Again math formula source is included in CAGR section of the code as comment.


Also, I couldn't get Divisior working (showing error message when 1 or more input field/s contains a value zero


Please see the attached latest project.

Post's attachments

Attachment icon Working on same form 3.zip 16.81 kb, 431 downloads since 2016-09-15 

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

Re: One Form MVD app

I think I got the Divisior bit working, not 100% sure tho.


However, on formula front no success. Math formula I'm trying implement is:


y = a(1 + r)^x


where:
y = future compounded value
a = initial value
r = growth rate
x = number of periods


I got the following object pascal implementation from another forum:


uses
  Math;
 
function CAGR(initial_value, grouth_rate, number_of_periods: double): double;
begin
  result := initial_value * Power(( 1 + grouth_rate), number_of_periods);
end;
 
begin
  writeln(CAGR(5.1, 10.4, 2.2):0:2);
  readln();
end.

Unfortunately, I couldn't implement it in MVD.

Post's attachments

Attachment icon Working on same form 4.zip 17.19 kb, 460 downloads since 2016-09-17 

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

Re: One Form MVD app

I can tell you how to imlement the formula but you will have to go thru the steps. the formula is y=a(1+r)^x

declare your variables.

set s = 1+r
set j = 1+r

for p= 1 to x-1
        n = j * s
         j = n
next p

y = a(n)

i think this will work for you

lee

18 (edited by AD1408 2016-09-17 22:13:13)

Re: One Form MVD app

Thanks  for the info Lee.....


I'm not clear about variables and formula in your explanation. Actual formula needs to be used in this project code is:



OutputFutureValue := InitialValue * (1 + GrowthRate) ^ Periods; 

Problem is I couldn't work out power (^) part in script.


Could you not put in project code which starts at line 200 or into code below?


//CALCULATIONS
procedure Form1_Button3_OnClick (Sender: string; var Cancel: boolean);
var    //---> First declare your variables (pay attention to the type, integers take no decimals, real can have decimals and so on ...)

    //INPUT VARIABLES
    InitialValue : Currency;
    Periods, GrowthRate : Real;

    //OUTPUT VARIABLES
    OutputFutureValue : Currency;


begin
    //---> Second, assign the variables you have declared to your edit fields
    //Input Variables
    InitialValue := Form1.EdInitialValue.Value;
    Periods := Form1.EdPeriods.Value;
    GrowthRate := Form1.EdGrowthRate.Value;


    if (Trim(Form1.EdInitialValue.Text) = '') OR  (Trim(Form1.EdPeriods.Text) = '') OR  (Trim(Form1.EdGrowthRate.Text) = '') then //if any of the field is empty
        begin
            Cancel := True; //Stop the operation
            ShowMessage('All fields marked with an asterisk (*) are required. Calculation can only be performed when all required fields are filled.'); //display the warning
        end

    else //None of the fields are empty, let's continue
        begin

            //---> Third, perform your calculations

                        OutputFutureValue := InitialValue * (1 + GrowthRate) ^ Periods;
                        Form1.EdOutputFutureValue.Value := OutputFutureValue;
Adam
God... please help me become the person my dog thinks I am.

Re: One Form MVD app

not going to be much hepin the coding department. i will try and get you started but you will have to look up the syntax of the for loop. i am assuming the ^ notation is a power like a number squared or cubed etc. anyway that is what the for loop does is take a number to a certain power.

set s = 1+r
set j = 1+r

for p= 1 to x-1
        n = j * s
         j = n
next p

y = a(j)

OutputFutureValue := InitialValue * (1 + GrowthRate) ^ Periods;


//CALCULATIONS
procedure Form1_Button3_OnClick (Sender: string; var Cancel: boolean);
var    //---> First declare your variables (pay attention to the type, integers take no decimals, real can have decimals and so on ...)

    //INPUT VARIABLES
    InitialValue : Currency;
    Periods, GrowthRate,s, j, n, p : Real;

    //OUTPUT VARIABLES
    OutputFutureValue : Currency;


begin
    //---> Second, assign the variables you have declared to your edit fields
    //Input Variables
    InitialValue := Form1.EdInitialValue.Value;
    Periods := Form1.EdPeriods.Value;
    GrowthRate := Form1.EdGrowthRate.Value;


    if (Trim(Form1.EdInitialValue.Text) = '') OR  (Trim(Form1.EdPeriods.Text) = '') OR  (Trim(Form1.EdGrowthRate.Text) = '') then //if any of the field is empty
        begin
            Cancel := True; //Stop the operation
            ShowMessage('All fields marked with an asterisk (*) are required. Calculation can only be performed when all required fields are filled.'); //display the warning
        end

    else //None of the fields are empty, let's continue
        begin

            //---> Third, perform your calculations
                        s := 1 + GrowthRate;
                        j := 1 + GrowthRate;
                        for p := 1 to (periods - 1)
                                  n := j * s
                                   j := n
                         next p;
                         OutputFutureValue := InitialValue * j;
//like i said, i am not sure of the coding and i am assuming ^ is a power symbol. i dont recal having run ito it before so i am not sure. hope this was of some help

lee
                        OutputFutureValue := InitialValue * (1 + GrowthRate) ^ Periods;
                        Form1.EdOutputFutureValue.Value := OutputFutureValue;

Re: One Form MVD app

Hi Lee,


I couldn't get your formula to compile. However, thank you very much for the effort.


Perhaps somebody here can offer the solution....................................

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

21 (edited by mathmathou 2016-09-19 03:51:48)

Re: One Form MVD app

Hello Adam,

I read the rest of the thread quite quickly, so I might answer a question you did not ask :-)

To raise a number to a certain power, you can not use the ^ sign. Instead, use the dedicated function which is :

Power(base,exponent);

In your case, it should give something like

Power(InitialValue * (1 + GrowthRate), Periods);

I think it's a fairly recent addition to MVD, added only recently by Dmitry. 2.6 or 2.7 maybe.


Good luck


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: One Form MVD app

that will do it a whole lot easier. i was doing it with a for/next loop which apparently neither one of us knows how to code in MVD

thanks
lee

Re: One Form MVD app

Hi Mathias,


Thanks a lot for the formula...


However, calculation result is wrong. I used on calc:
Initial value 1000
Number of Periods 10
Compound Growth Rate 10
Calculated result for Future Compounded Value: 3


Calculated result should have been: 2593.7424601


I have used your formula as below:

OutputFutureValue := Power(InitialValue * (1 + GrowthRate), Periods);
Adam
God... please help me become the person my dog thinks I am.

Re: One Form MVD app

Hi.
i think it should be OutputFuturevalue := InitalValue * Power((1 + GrowthRate), Periods);

the other way included initalvalue in the power and i dont think that is what he wanted.

lee

Re: One Form MVD app

Hi Lee,


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


That works.


Now I need to adjust the formula so that when user enters "Compound Growth Rate" as 10 (%)  it'd convert to 0.1 and use it in calc. converted value.


I also need to the formula for calculating "Compound Growth Rate" (second part of CAGR calculator)..  I'll have a go at it.

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