I have a form in excel that includes a large number of controls. For many of the controls, there is an _AfterUpdate event that fires correctly when the user enters data. The _AfterUpdate code makes entries in one or more ranges in the associated spreadsheet (I am not using the ControlSource properties for these controls). Calculations are performed correctly in the spreadsheet on the basis of the changed values. Finally, at the end of each of these _AfterUpdate routines there is a call to a frmMealPlanAdditionsUpdate procedure to repopulate the controls on the form based on the current values of various ranges in the spreadsheet and the results show up in the form correctly as the user continues working. So far, so good.
I have included a Clear button on the form that has a _Click event for which the code is included below. This works to the point of clearing the contents of the form. When using the form, all the controls are cleared when the Clear button is clicked. However, the associated ranges in the spreadsheet have not been affected (I can see them on the screen while the form is visible).
Apparently, the _AfterUpdate events are not being triggered. I have checked this by including breaks in the code below and in the associated _AfterUpdate procedures. I also tried a variety of ways to call the _AfterUpdate directly, but have not found anything that gets past the debugger when executing. In this respect, the behavior is the same for text boxes and combo boxes. I have also tried changing to _Change; but, that led to a bunch of other problems and I came back to using _AfterUpdate.
The code below for is in the module for the form (i.e. the one that shows up when I double click a control in the VBE). I also included the code for one of the _AfterUpdate procedures. The code for the frmMealPlanAdditionsUpdate procedure is rather long, so I just included the line that is affected by the txtFoodGramsCarbohydrate_AfterUpdate procedure as an example.
I must be missing something fundamental because various forms of the approach to looping through all the controls have been posted in lots of places for years. Nobody complains that the values come back. In my case, I want some of the values on the form to be a function of other values that have been entered. In general, the form does this well when I use the form manually. I can even clear the controls manually and the _AfterUpdate procedures are called. However, changing the values for the controls using the btnClear_Click does not. I suspect that making the changes using VBA in another context will not trigger the _AfterUpdate either.
I been at this for a while now and I'm out of clues. Any help will be appreciated.
Bruce
I have included a Clear button on the form that has a _Click event for which the code is included below. This works to the point of clearing the contents of the form. When using the form, all the controls are cleared when the Clear button is clicked. However, the associated ranges in the spreadsheet have not been affected (I can see them on the screen while the form is visible).
Apparently, the _AfterUpdate events are not being triggered. I have checked this by including breaks in the code below and in the associated _AfterUpdate procedures. I also tried a variety of ways to call the _AfterUpdate directly, but have not found anything that gets past the debugger when executing. In this respect, the behavior is the same for text boxes and combo boxes. I have also tried changing to _Change; but, that led to a bunch of other problems and I came back to using _AfterUpdate.
The code below for is in the module for the form (i.e. the one that shows up when I double click a control in the VBE). I also included the code for one of the _AfterUpdate procedures. The code for the frmMealPlanAdditionsUpdate procedure is rather long, so I just included the line that is affected by the txtFoodGramsCarbohydrate_AfterUpdate procedure as an example.
I must be missing something fundamental because various forms of the approach to looping through all the controls have been posted in lots of places for years. Nobody complains that the values come back. In my case, I want some of the values on the form to be a function of other values that have been entered. In general, the form does this well when I use the form manually. I can even clear the controls manually and the _AfterUpdate procedures are called. However, changing the values for the controls using the btnClear_Click does not. I suspect that making the changes using VBA in another context will not trigger the _AfterUpdate either.
I been at this for a while now and I'm out of clues. Any help will be appreciated.
Bruce
Code:
Private Sub btnClear_Click()
'Clears all of the controls on the form that the user can change.
Dim ctl As MSForms.Control ' was just "contol" instead of "MSForms.Control"
For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "TextBox"
ctl.Text = ""
Case "CheckBox", "OptionButton", "ToggleButton"
ctl.value = False
Case "ComboBox", "ListBox"
ctl.ListIndex = -1
End Select
Next ctl
End Sub
Private Sub txtFoodGramsCarbohydrate_AfterUpdate()
If txtFoodGramsCarbohydrate.Text = vbNullString Then
Range("FoodGramsCarbohydrateInput").value = txtFoodGramsCarbohydrate.Text
Else
Range("FoodGramsCarbohydrateInput").value = CDbl(txtFoodGramsCarbohydrate.Text)
End If
Call frmMealPlanAdditionsUpdate
End Sub
Public Sub frmMealPlanAdditionsUpdate()
' Transfers information to the form.
' Do stuff with all the form data.
txtFoodGramsCarbohydrate.Text = Format(CStr(Range("FoodGramsCarbohydrateInput").value), "0.00")
' Do more stuff with all the form data.
End Sub