Calculating using Field Data - with thanks to Jodstar for the suggestions.

Two methods are presented below:

1. Expressions
2. Visual Basic

Both techniques are presented on the same Form. Using the "expression approach is simpler, but not as flexible as the Visual Basic approach.

1. Expressions ...

STEP 1    Set up a sample Table containing the following Field Names and Data Types ...

STEP 2    Set up the following Form ...

STEP 3    Go to the Properties of Average (Right-click the Average control.) Enter the following "expression" into the Control Source of the control Average  (ie =([Value1] + [Value2] + [Value3])/3 ...

You have finished setting up your expression.

2. Visual Basic ...

STEP 4    Go into the Properties window of the Calculate Command Button. (ie double-click the button) Click in the On Click field and then click the down arrow. Select [Event Procedure]. next click the "..." to go to the Visual Basic window.

Clicking the "..." takes you to the Visual Basic window ...

STEP 5    Enter the following code ... (See below for the "me" syntax.)

You have finished setting up your Visual Basic calculation.

STEP 6    Return to the Access Form by clicking on the bottom Task Bar.

You now have a Form that can carry out calculations based on Field Data using either the expression technique, or Visual Basic.

 

Referring to Forms in VB Code ...

The example above used the "Me" object name. It may also be entered as "Me!". Using "me" means "This Form". ie the Form containing the control to which the code is attached. The alternative approach is to use the syntax: 

Forms!<FormName>

Using the example above the code would become:

Private Sub Calculate_Click()
SetFocus
Forms!Form1.AverageB = (Forms!Form1.ValueB1 + Forms!Form1.ValueB2 + Forms!Form1.ValueB3) / 3 
End Sub

The advantage of this approach is that you can use data from a number of different Forms in your calculations.

<- Back