How to use computed field in Microsoft Ligthswitch?

Below is an example of a computed field in action.  I use this to compute for the balance of a loan from a financing company.  A single loan amount (SI Amount) has multiple adjustments through Credit / Debit Memo records and multiple payments through collection records (OR Amount).

The first step in creating a computed field is to open the EF table design and add a Computed Property (#1 in the screen below).  Every time you add a computed property it will insert a field into your EF table with a calculator icon in the left side (#2 in the screen below).  And the last step is to provide the computation of each field, to do that just click any calculated field and click the Edit Method link in the right side property box (#3 in the screen below).

The code is very simple, just compute for a value to the result (return) variable provided by the method (see sample code below).  Thats it.

Private Sub BalanceAmountComputation_Compute(ByRef result As Decimal)
   'Set result to the desired field value
   result = IIf(IsNothing(Me.SIAmount), 0, Me.SIAmount) + _
            IIf(IsNothing(Me.DMAmountComputation), 0, Me.DMAmountComputation) - _
            IIf(IsNothing(Me.CMAmountComputation), 0, Me.CMAmountComputation) - _
            IIf(IsNothing(Me.ORAmountComputation), 0, Me.ORAmountComputation)
End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s