Topic: Perfom calculations between to text fields

Hi everybody

I'm very new with Delphi language so I will need all the help I can get in order to keep using "MyVisualDatabase"

I came from XOJO and there are a few difference between those languages;

if I want to perform a calculation between text fields in xojo the code is looking like this

dim number_1 as double = val(textfield_1.text)
dim number_2 as double = val(textfield_2.text)
dim total as double

dim result as string = textfiled_3.text

total = number_1 + number_2

result = format(total,"0.0")

So my question is; how I can achieve the same with Delphi?

Many thanks for any suggestion

Sincerely

2 (edited by derek 2020-11-26 16:43:18)

Re: Perfom calculations between to text fields

Hi,
Basically, there are 2 ways to do this.
1.
Use a calculated field (see Webapp1 in the attachment - calculated fields are defined in the same place as your data structure but are 'virtual' fields - they only exist while the program is running).  This is the most data efficient way as the result is not saved to the database - the result is simply calculated whenever required by the program.  The only down side to this is when you add or edit a record, the result of the calculation is not seen (in Form2) until the record has been saved.  The result is, however, shown immediately in the relevant tablegrid (Form1).
2.
Define a field in your table to hold the result of the calculation.  Then add a small script to recalculate the result whenever any of the relevant fields are changed (see Webapp2 in the attachment).  This way, you always see immediately what the result of the calculation is in both Form1 and Form2 but at the expense of some data redundancy.
.
There is actually, a third way (which would be my personal preference) which is a combination of the above - use a calculated field (so there is no data redundancy) and use the same script to update the result of the calculation immediately in both forms (see Webapp3 in the attachment).
.
It's actually a lot easier just to see it working than it is to try and describe it - LOL!  Any questions, just shout.
Regards.
Derek.

Post's attachments

Attachment icon webapp.zip 1011.17 kb, 210 downloads since 2020-11-26 

Re: Perfom calculations between to text fields

Hi Derek

Thank you, for your tips. As I understand you suggest working directly on DB fields, which is working perfectly. Please correct me if I'm wrong.

As follow im posting my workaround that fit (my guess) my needs.

procedure main_txtTotale_OnClick (Sender: TObject);

var
testo: string;
              prezzo, quantita, totale: real;


begin
                       prezzo := StrToFloat(main.txtPrezzoCliente.text);
                          quantita := StrToFloat(main.txtQuantita.text);
                                   totale:= prezzo * quantita ;

testo := FormatFloat('0.#####', totale);

main.txtTotale.text := testo;

end;

Work perfectly for my needs and very thank you for your tips.