When to use afterupdate rather than exit event

Michael Simpson

Board Regular
Joined
Jun 21, 2010
Messages
232
Thought I'd start this topic since there seem to be a number of topics where the answer seems to be to use one of the above rather than other. Thought I'd kick off with my 2 cents' worth.

I have a userform with frames containing textboxes. The user enters a currency value and once they leave the control, then a protected textbox next to it shows the corresponding value in SEK. I started off using the exit event but ran into 2 problems.


  1. If you tabbed out of the last textbox in the frame, the exit event never kicked in (this is documented in other topics but took some time to find). This resulted in me using the exit event for all except the last textbox in the frame that used afterupdate instead
  2. I then discovered that the exit events didn't kick in if, instead of tabbing out of the field, I deliberately placed focus in a control elsewhere on the form. Changing the event from exit to afterupdate corrected this.
My question then is ... could you guys document in this topic when you would/must use the exit rather than the afterupdate event (or vice-versa).

Thanks
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
The Exit event has a Cancel argument that you can use to return to the TextBox if, for example, the entry is invalid.
 
Upvote 0
But surely, it would be just as easy to use .setfocus in the afterupdate event if all you need to do is return focus to your control ?
 
Upvote 0
Did you try that? In the code below the first sub doesn't work but the second does:

Code:
Private Sub TextBox1_AfterUpdate()
    With TextBox1
        If Len(.Value) = 0 Then
            .SetFocus
        End If
    End With
End Sub
 
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    With TextBox2
        If Len(.Value) = 0 Then
            Cancel = True
        End If
    End With
End Sub
 
Upvote 0
My bad Andrew

I think there might have been a misunderstanding between us. I meant the following use of setfocus (I just didn't bother to include the control name)
Code:
Private Sub Short_term_entry_afterupdate()

    Dim Lvalue As Boolean
    Dim belopp As Currency

    Lvalue = IsNumeric(Short_term_entry.Value)
    If Lvalue = True Then
        belopp = Short_term_entry.Value
    End If

    Select Case True
        Case Lvalue = False
            MsgBox "Felaktigt värde"
            [COLOR=Red]Short_term_entry.SetFocus[/COLOR]
    etc etc etc
 
Upvote 0
Re: My bad Andrew

With that code I see the message box but focus doesn't return to the control. Does it for you?
 
Upvote 0
**** !!!!

You're quite right Andrew - it doesn't. In that case my original topic title is even more appropriate.

However, as I mentioned in the starter append, if I enter an invalid numeric value into the control and place focus on another control (without tabbing out of the first one), then the exit event never kicks in.

So I guess my topic title still stands - when to use one event rather than the other
 
Upvote 0
Re: **** !!!!

I received your workbook. I can't test it because the UserForm contains some objects that I don't have.

But you can't SetFocus in AfterUpdate because the event fires before focus is set to the next control. SetFocus works, but then the focus moves.

I see you have:

Private Sub Limit_belopp_entry_exit2(ByVal Cancel As MSForms.ReturnBoolean)

You can't change the name of an event procedure like that. It should be:

Private Sub Limit_belopp_entry_Exit(ByVal Cancel As MSForms.ReturnBoolean)
 
Upvote 0

Forum statistics

Threads
1,215,222
Messages
6,123,709
Members
449,118
Latest member
MichealRed

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top