View Full Version : Calculating a Field


crhodus
06-21-2001, 12:51 PM
I need to have a field calculated on one of my forms. After the user enters in data into the fields STARTINGSALLARY and HIGHESTSALLARY, I need the field AVGSALLARY to store and display the average of STARTINGSALLARY and HIGHESTSALLARY.

I tried putting =Avg(STARTINGSALLARY + HIGHESTSALLARY) in the default value property for AVGSALLARY, but this did not work.

Can anyone tell me how I can do this?

Thanks!

dhoffman
06-21-2001, 01:23 PM
THe way I would do it, and it may not be the best way, is to add some code to the AfterUpdate event of HIGHESTSALLRY. Like This:
Me.[AVGSALLARY]= (STARTINGSALLARY + HIGHESTSALLARY) / 2

But you would need to ensure that STARTINGSALLARY is entered before HIGHEST. You could add similar code to the AfterUpdate of both START and HIGHEST, but in both first check to see if the other has been entered yet.
(like for highest's afterupdate)
IF Not (IsNull(STARTING) and Len(START)>0) Then
Me.[AVGSALLARY]= (STARTINGSALLARY + HIGHESTSALLARY) / 2

and vice versa for Start

dhoffman
06-21-2001, 01:27 PM
The default property won't work because the default value for each field is used as soon as the record becomes current or something like that, so it has no effect while a record is being edited. I'm sure you have to explicitly set the desired field after the needed data has been entered.

crhodus
06-22-2001, 08:36 AM
Thanks! I went with the If Then statement and it seems to be working.

Rich
06-22-2001, 09:54 AM
Storing the results of a calculation in this manner is not recommended in a relational database, use a query or simply use an unbound control to display the results.

dhoffman
06-22-2001, 10:50 AM
That's true, but you still might want to use an unbound textbox on the form to display this data to the user as a convenience.