I have noticed that when a form initializes, if a textbox control's value is changed in the initialization procedure and that control immediately receives focus, that control's AfterUpdate event fires. However, if debugging is used to step through the initialization and activation of the form, the event does not fire. Why not? This behavior is not limited to the initialization procedure, but more generally applies any time a procedure modifies another textbox's value, and that textbox then receives focus via tab/enter or .SetFocus. Again, when stepping through the procedure, this behavior is not observed. Why not? Is there any way I can prevent this behavior?
To duplicate: Create a user form with two textboxes. UserForm1 code:
Private Sub UserForm_Initialize() TextBox1 = "Initialized!" TextBox2 = "Initialized!" ' TextBox2.SetFocus End Sub Private Sub TextBox1_AfterUpdate() MsgBox "TextBox1 AfterUpdate Event Triggered" Unload Me End Sub Private Sub TextBox2_AfterUpdate() MsgBox "TextBox2 AfterUpdate Event Triggered" Unload Me End Sub
Showing this form causes TextBox1_AfterUpdate() to run, unless a breakpoint is placed in the UserForm_Initialize() procedure. Changing the tab order, or uncommenting TextBox2.SetFocus causes TextBox2_AfterUpdate()