I'm having trouble getting the AfterUpdate and LostFocus events of a text box on a subform to work properly (at least to work as I expect).
The textbox in question is one called Compare_Ratio in the Child_Node_Compare_Datasheet subform within the Nodes_In_This_Project form: [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Child_Node_2_SK]. When I update this field, I want to call a subroutine (called UpdtCompareRatioInverse) in the AfterUpdate event of the Compare_Ratio text field. UpdtCompareRatioInverse should update the value in the Compare_Ratio field in a different, but related record with the reciprocal of the just updated value of the Compare_Ratio field.
I expect that AfterUpdate event to be triggered when I change the value in the Compare_Ratio field and then either
1) move the cursor to another field in the same record on the subform,
2) hit ENTER,
3) move the cursor to the Compare_Ratio field in another record of the subform, or
4) click a botton on the parent form (Nodes_In_This_Project) that calls another subroutine that is dependent on UpdtCompareRatioInverse having completed successfully.
I get different results, depending on whether I a) program UpdtCompareRatioInverse to be called from Compare_Ratio_AfterUpdate() or Compare_Ratio_LostFocus() and b) whether I do 1) or 2) above vs 3) or 4). The results are as follows:
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_AfterUpdate() and do either 1) or 2):
-- the AfterUpdate event occurs, but the value of the Compare_Ratio field in the record that I tried to change remains unchanged and
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the unchanged value.
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_AfterUpdate() and do either 3) or 4):
-- the AfterUpdate event occurs, and the value of the Compare_Ratio field in the record that I tried to change remains changes, but
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the previous/unchanged value.
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_LostFocus() and do either 1) or 2):
-- the AfterUpdate event occurs, and the value of the Compare_Ratio field in the record that I tried to change remains changes, but
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the previous/unchanged value.
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_LostFocus() and do either 3) or 4), things work as they should:
-- the AfterUpdate event occurs, and the value of the Compare_Ratio field in the record that I tried to change remains changes, but
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the new value.
Can you help me understand:
1) Why calling UpdtCompareRatioInverse from Compare_Ratio_AfterUpdate() does not work at all, but calling it from Compare_Ratio_LostFocus() does work for 3) or 4).
2) Why doing 1) or 2) causes UpdtCompareRatioInverse not to work (when called from Compare_Ratio_AfterUpdate()), but 3) or 4) do cause UpdtCompareRatioInverse to work under the same circumstances.
The code for UpdtCompareRatioInverse and the two event subroutines is as follows:
Private Sub UpdtCompareRatioInverse()
Dim wrkSpace As Workspace
Dim dBase As Database
Dim RecSet As Recordset
Dim Source_Ratio As Single
Dim intProject_SK, intParent_Node_SK, intChild_Node_1_SK, intChild_Node_2_SK As Integer
On Error GoTo TransactionFailed
intProject_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Project_SK]
intParent_Node_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Parent_Node_SK]
intChild_Node_1_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Child_Node_1_SK]
intChild_Node_2_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Child_Node_2_SK]
Set wrkSpace = DBEngine.Workspaces(0)
Set dBase = wrkSpace.Databases(0)
Set RecSet = dBase.OpenRecordset("SELECT Child_Node_Compare.Compare_Ratio FROM Child_Node_Compare " & _
" WHERE (((Child_Node_Compare.Project_SK)= " & intProject_SK & ") " & _
" AND ((Child_Node_Compare.Parent_Node_SK)= " & intParent_Node_SK & ") " & _
" AND ((Child_Node_Compare.Child_Node_1_SK)= " & intChild_Node_1_SK & ") " & _
" AND ((Child_Node_Compare.Child_Node_2_SK)= " & intChild_Node_2_SK & "))")
Source_Ratio = RecSet![Compare_Ratio]
Source_Ratio = 1 / Source_Ratio
MsgBox "Compare Ratio Inverse: " & Source_Ratio
dBase.Execute "UPDATE Child_Node_Compare SET Child_Node_Compare.Compare_Ratio = " & Source_Ratio & " " & _
"WHERE (((Child_Node_Compare.Project_SK)= " & intProject_SK & ") " & _
"AND ((Child_Node_Compare.Parent_Node_SK)= " & intParent_Node_SK & ") " & _
"AND ((Child_Node_Compare.Child_Node_2_SK)= " & intChild_Node_1_SK & ") " & _
"AND ((Child_Node_Compare.Child_Node_1_SK)= " & intChild_Node_2_SK & "))"
RecSet.Close
Exit Sub
TransactionFailed:
MsgBox "Error #: " & Err & ": " & Error(Err)
wrkSpace.Rollback 'Rollback if any execute fails
Exit Sub
End Sub
Private Sub Compare_Ratio_AfterUpdate()
MsgBox ("AfterUpdate Started")
'UpdtCompareRatioInverse
End Sub
Private Sub Compare_Ratio_LostFocus()
MsgBox ("LostFocus Started ")
UpdtCompareRatioInverse
End Sub
At any given time, I only call UpdtCompareRatioInverse from Compare_Ratio_AfterUpdate() or Compare_Ratio_LostFocus(), but not from both.
Your thoughts?
The textbox in question is one called Compare_Ratio in the Child_Node_Compare_Datasheet subform within the Nodes_In_This_Project form: [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Child_Node_2_SK]. When I update this field, I want to call a subroutine (called UpdtCompareRatioInverse) in the AfterUpdate event of the Compare_Ratio text field. UpdtCompareRatioInverse should update the value in the Compare_Ratio field in a different, but related record with the reciprocal of the just updated value of the Compare_Ratio field.
I expect that AfterUpdate event to be triggered when I change the value in the Compare_Ratio field and then either
1) move the cursor to another field in the same record on the subform,
2) hit ENTER,
3) move the cursor to the Compare_Ratio field in another record of the subform, or
4) click a botton on the parent form (Nodes_In_This_Project) that calls another subroutine that is dependent on UpdtCompareRatioInverse having completed successfully.
I get different results, depending on whether I a) program UpdtCompareRatioInverse to be called from Compare_Ratio_AfterUpdate() or Compare_Ratio_LostFocus() and b) whether I do 1) or 2) above vs 3) or 4). The results are as follows:
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_AfterUpdate() and do either 1) or 2):
-- the AfterUpdate event occurs, but the value of the Compare_Ratio field in the record that I tried to change remains unchanged and
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the unchanged value.
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_AfterUpdate() and do either 3) or 4):
-- the AfterUpdate event occurs, and the value of the Compare_Ratio field in the record that I tried to change remains changes, but
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the previous/unchanged value.
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_LostFocus() and do either 1) or 2):
-- the AfterUpdate event occurs, and the value of the Compare_Ratio field in the record that I tried to change remains changes, but
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the previous/unchanged value.
When I program UpdtCompareRatioInverse to be called from Sub Compare_Ratio_LostFocus() and do either 3) or 4), things work as they should:
-- the AfterUpdate event occurs, and the value of the Compare_Ratio field in the record that I tried to change remains changes, but
-- UpdtCompareRatioInverse updates the related record with the reciprocal of the new value.
Can you help me understand:
1) Why calling UpdtCompareRatioInverse from Compare_Ratio_AfterUpdate() does not work at all, but calling it from Compare_Ratio_LostFocus() does work for 3) or 4).
2) Why doing 1) or 2) causes UpdtCompareRatioInverse not to work (when called from Compare_Ratio_AfterUpdate()), but 3) or 4) do cause UpdtCompareRatioInverse to work under the same circumstances.
The code for UpdtCompareRatioInverse and the two event subroutines is as follows:
Private Sub UpdtCompareRatioInverse()
Dim wrkSpace As Workspace
Dim dBase As Database
Dim RecSet As Recordset
Dim Source_Ratio As Single
Dim intProject_SK, intParent_Node_SK, intChild_Node_1_SK, intChild_Node_2_SK As Integer
On Error GoTo TransactionFailed
intProject_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Project_SK]
intParent_Node_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Parent_Node_SK]
intChild_Node_1_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Child_Node_1_SK]
intChild_Node_2_SK = [Forms]![Nodes_In_This_Project]![Child_Node_Compare_Datasheet].[Form]![Child_Node_2_SK]
Set wrkSpace = DBEngine.Workspaces(0)
Set dBase = wrkSpace.Databases(0)
Set RecSet = dBase.OpenRecordset("SELECT Child_Node_Compare.Compare_Ratio FROM Child_Node_Compare " & _
" WHERE (((Child_Node_Compare.Project_SK)= " & intProject_SK & ") " & _
" AND ((Child_Node_Compare.Parent_Node_SK)= " & intParent_Node_SK & ") " & _
" AND ((Child_Node_Compare.Child_Node_1_SK)= " & intChild_Node_1_SK & ") " & _
" AND ((Child_Node_Compare.Child_Node_2_SK)= " & intChild_Node_2_SK & "))")
Source_Ratio = RecSet![Compare_Ratio]
Source_Ratio = 1 / Source_Ratio
MsgBox "Compare Ratio Inverse: " & Source_Ratio
dBase.Execute "UPDATE Child_Node_Compare SET Child_Node_Compare.Compare_Ratio = " & Source_Ratio & " " & _
"WHERE (((Child_Node_Compare.Project_SK)= " & intProject_SK & ") " & _
"AND ((Child_Node_Compare.Parent_Node_SK)= " & intParent_Node_SK & ") " & _
"AND ((Child_Node_Compare.Child_Node_2_SK)= " & intChild_Node_1_SK & ") " & _
"AND ((Child_Node_Compare.Child_Node_1_SK)= " & intChild_Node_2_SK & "))"
RecSet.Close
Exit Sub
TransactionFailed:
MsgBox "Error #: " & Err & ": " & Error(Err)
wrkSpace.Rollback 'Rollback if any execute fails
Exit Sub
End Sub
Private Sub Compare_Ratio_AfterUpdate()
MsgBox ("AfterUpdate Started")
'UpdtCompareRatioInverse
End Sub
Private Sub Compare_Ratio_LostFocus()
MsgBox ("LostFocus Started ")
UpdtCompareRatioInverse
End Sub
At any given time, I only call UpdtCompareRatioInverse from Compare_Ratio_AfterUpdate() or Compare_Ratio_LostFocus(), but not from both.
Your thoughts?