PDA

View Full Version : [SOLVED:] AfterUpdate not triggered



SSW
05-06-2015, 01:34 PM
Hi All,

I am new to VBA and hope somebody here will be able to help me.
I have a form with just one text box. I have a code which should trigger once the textbox value is changed.
Currently, once I enter a value and press enter, it doesnt trigger the event.
The event is triggered only when I close the form.

I read that the event is triggered only when the focus is changed from the text box. Does that mean I need to have more than 1 text box?
Why doesn't pressing 'Enter' key trigger the event?

Thanks!!

jonh
05-07-2015, 01:05 AM
How would it know you've done editing until you leave the control?
If you want to run code at each chage in value use the change event.

You can also check if enter has been pressed...


Dim updated As Boolean

Private Sub TextBox1_AfterUpdate()
If Not updated Then
MsgBox "updated"
updated = True
End If
End Sub

Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Debug.Print KeyCode 'prints keycode to immediate window
Select Case KeyCode
Case 13 'enter key
KeyCode = 0 'cancel
TextBox1_AfterUpdate
Case Else
updated = False
End Select
End Sub

Enter usually adds a new line to text or jumps (passes focus) to the next contol in the tab order. If multiline isn't turned on for the textbox or there are no other controls to tab to, Enter isn't going to do anything.

mperrah
05-08-2015, 10:49 AM
What are trying to trigger after "enter"
why not call the macro from a button on the form
label it run or calculate...

SSW
05-12-2015, 12:58 PM
Thanks for both the replies. I tried it with KeyUp event. It works exactly as I wanted !!!!!
However, in order to avoid duplicate processing, I had to insert a check to see if the form is closed (Under QueryClose). Otherwise it was triggering the afterupdate again when the form is closed.

Now, to make things simple, I decided to use a command button!!!