'After input in text box how do cause AfterUpdate event to run?
I have an Excel VBA userform with several text boxes.
The user will input a weight in a text box. They can then do other things on the form or click Apply, Update, Previous Event, Next Event or Cancel.
After the weight is input, it must be validated, and if it is OK, the form is marked as mbFormChanged=True. This validation occurs in the text box's AfterUpdate event.
My problem is that if the user types in a value and immediately clicks Apply, Update, Previous Event or Next Event, the field is not validated and it is as if it was never changed.
I.e enter weight: 200[Update]
However, if the user tabs to another field after typing the weight, then it is validated and the form is marked as changed.
I.e. enter weight: 200[Tab][Update]
How can I make sure AfterUpdate runs when a command button is clicked immediately thereafter?
I can't put the validation at the point of OK/Apply because the moment a user enters a new weight and it is valid, it immediately updates many other fields and lists visible on the form (real time update).
Solution 1:[1]
I assume this is an Excel user form in which case I don't think there is a built in AfterUpdate event. Excel forms are not bound to anything (unlike Access) so there is nothing the form updates. You'll need to make your own AfterUpdate event.
Solution 2:[2]
I know this is a an old/dead thread and I certainly hope the original problem has been solved, but in case anyone else is searching this, here is what I have done in similar situations: Open your form with the 'Ok' / 'Done' / 'Exit" or whatever button(s) you are using to confirm the form entries then exit the form in a disabled state. That way they do nothing if clicked on. Once your data validation is complete, enable those buttons (or circle back for corrected entries).
Solution 3:[3]
As commented, you can try this:
Private Sub TextBox1_Change()
If (MsgBox("Done?", vbYesNo)) = vbYes Then Me.TextBox2.SetFocus 'Or any other ctrl
End Sub
Above code like forces TextBox1_AfterUpdate() event.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Brad |
| Solution 2 | Karl Reighard |
| Solution 3 | L42 |
