Calculated values embedded in table (1 Viewer)

mtagliaferri

Registered User.
Local time
Today, 17:03
Joined
Jul 16, 2006
Messages
538
I am trying to make some calculations in a form, and the result od the calculation being written as a result in a table, I am aware that a query would be the best approach however I need some calculated values available in the form.

I have a field NetPay in my table where I want to store the sum of: [BasicSalary]+[AdditionalAllowances]+[Adjustment]-[PensionContributions]-[TaxPaid]-[NI].

Any guidance on this would be appreciated.
 
if all those on your Expression are fieldname, you can put the Expression on NetPay (Calculated field).
 
if all those on your Expression are fieldname, you can put the Expression on NetPay (Calculated field).
Yes they are all Fieldname, you mean to change NetPay in my table to Calculated Value and place the formula there?
 
Yes they are all Fieldname, you mean to change NetPay in my table to Calculated
NetPay should be Calculated field, then place your formula there

you can use this Expression:

Code:
Iif(IsNull([BasicSalary]), 0, [BasicSalary]) +
Iif(IsNull([AdditionalAllowances]), 0, [AdditionalAllowances]) +
Iif(IsNull([Adjustment]), 0, [Adjustment]) -
Iif(IsNull([PensionContributions]), 0, [PensionContributions]) -
Iif(IsNull([TaxPaid]), 0, [TaxPaid]) -
Iif(IsNull([NI]), 0, [NI])

i use Iif() function because if you have Null value on either
one of those field,s you will get unexpected result.
 
Can't you use Nz() to make it a bit more versbose?

Code:
Nz(BasicSalary,0) + Nz(AdditionalAllowance,0) etc. etc.
 
[BasicSalary]+[AdditionalAllowances]+[Adjustment]-[PensionContributions]-[TaxPaid]

Are all those fields in a table? Or are they just controls on an unbound form?
 
I am aware that a query would be the best approach however I need some calculated values available in the form.

You can - and probably should - use a query to drive a form anyway. Forms will happily use anything that produces a recordset, and that includes tables and SELECT queries. In fact, even for single-table forms, I have found that if you are dealing with a linked back-end file, sometimes you get better results with a query. Sometimes if you use a direct-to-table-via-external-link, you get asked "Where is table XXX?" when opening the form - but the use of a query bypasses that question.

Not only that, but if you wanted a computed value on your form (or a report), you can define the .ControlSource for the particular computation control to be an expression that adds the multiple fields together, in which case they would be computed at the time of the Form_Current event. As long as the multiple fields are available in the form's .Recordset, you can reference them even if they are NOT individually represented by controls on the form.
 

Users who are viewing this thread

Back
Top Bottom