Question : MS Access Where to put the code to calculate a field using data from a record selected in a subform

Hello, I need to run the attached code and put the results in the item name, met value and the calculated burned value in the fields shown in the red rectangle on the attached form (image) when a record (green circle) on the subform is clicked.
On what event on which control should the code be placed. Thank you
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
If Forms!caloriesbyactivityform!Age = 0 Then
MsgBox "You must enter your Age in Years"
Forms!caloriesbyactivityform![Age].SetFocus
Exit Sub
End If

If Forms!caloriesbyactivityform!feet = 0 Then
MsgBox "You must enter your Height in Feet and Inches"
Forms!caloriesbyactivityform![feet].SetFocus
Exit Sub
End If

If Forms!caloriesbyactivityform!weight = 0 Then
MsgBox "You must enter your Weight"
Forms!caloriesbyactivityform![weight].SetFocus
Exit Sub
End If

If Forms!caloriesbyactivityform!Duration = 0 Then
MsgBox "You must enter a Duration in Minutes"
Forms!caloriesbyactivityform![Duration].SetFocus
Exit Sub
End If

If Forms!caloriesbyactivityform!Male = True Then
MyAge = Forms!caloriesbyactivityform!Age
MyHeight = ((Forms!caloriesbyactivityform!feet * 12) + Forms!caloriesbyactivityform!inches) * 2.54
MyWeight = Forms!caloriesbyactivityform!weight / 2.2
MyBMR = 66 + (13.7 * MyWeight) + (5 * MyHeight) - (6.8 * MyAge)
Forms!caloriesbyactivityform!BMR = MyBMR
Else
MyAge = Forms!caloriesbyactivityform!Age
MyHeight = ((Forms!caloriesbyactivityform!feet * 12) + Forms!caloriesbyactivityform!inches) * 2.54
MyWeight = Forms!caloriesbyactivityform!weight / 2.2
MyBMR = 655 + (9.6 * MyWeight) + (1.8 * MyHeight) - (4.7 * MyAge)
Forms!caloriesbyactivityform!BMR = MyBMR
End If

StField = Me.metID
MyCriteria = "[metID] = " & StField
METValue = DLookup("[metnumber]", "MetabolicEquivalents", MyCriteria)
ItemSValue = DLookup("[listing]", "MetabolicEquivalents", MyCriteria)

MyMetValue = METValue * (MyBMR / 1440) * Forms!caloriesbyactivityform!Duration
Forms!caloriesbyactivityform!METValue1 = METValue
Forms!caloriesbyactivityform!Burned = MyMetValue
Forms!caloriesbyactivityform!ItemSelected = ItemSValue

Answer : MS Access Where to put the code to calculate a field using data from a record selected in a subform

The Current event fires each time you move between records (or add a new record), so that might be your best Event to use.
Random Solutions  
 
programming4us programming4us