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
 
Thanks Andrew. The exit2 event was just so I could flip-flop between either an afterupdate event or an exit one. By adding/removing the 2 I can debug each one
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
My basic problem is how to capture a change in a control when that control is one of a number within a frame. (I've read about this but can't find the correct URL now). Basically, if you have controls within a frame and tab through them, then tabbing out of the last control (ie, tabbing to a control outside the frame) will not cause the exit event to kick in for the last control.

I'm experimenting with all sorts of variations, but none seem to do what I really want.

In reality, what I really want to do is trivial (and should be coding-wise). For any control, after the user enters a value and tabs out of it (or mouse-clicks in another control), check the validity of the entered data. If invalid, issue a message and place focus back in the offending control. How difficult SHOULD that be ???
 
Upvote 0
Another thing you could try is using the Frame's Exit event. You can get which control was exited:

Code:
Private Sub Frame1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    MsgBox Frame1.ActiveControl.Name
End Sub
 
Upvote 0
The bug is documented here, with a suggested workaround:

http://support.microsoft.com/kb/210734

Granted, I haven't been able to test this work-around, but, off the cuff, it seems a strange one. I wonder what will happen if I press the command button because I want to. Surely, all it's doing is kicking in the exit event for textbox1, but I might not even have been there. Assume I have 2 combo boxes outside the frame (one "before" the frame, and one "after").

I enter a value in the first combo, place mouse in textbox2, enter something there, place cursor in 2nd combo, select something there and then press the command button.

If I understand the logic from MS's site, it will place the cursor back into textbox2 (thus kicking off the textbox1 exit event ?) and ...... ???

On Monday (back at work), I'll try and see if ungrouping the controls in the frame helps. I've tried all sorts of variations (adding/removing tab stops for the frame, disabling the frame etc) but none seem to help.
 
Upvote 0
Did you try using the Frame's Exit event?
I did, trouble is, using the following code
Code:
Private Sub Limit_frame_exit(ByVal Cancel As MSForms.ReturnBoolean)

    Dim Lvalue As Boolean
    Dim belopp As Currency

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

    Select Case True
        Case Lvalue = False
            MsgBox "Invalid value"
            Cancel = True
        Case belopp < 0
            MsgBox "Muse be >= 0l"
            Cancel = True
        Case Else
            ' All okay - get the CURRENT currency and its exchange rate, then calculate
            ' what the new value in long term belopp should be
            Call calculate_long_term_belopp_sek(belopp, "")

    End Select
End Sub
results in focus being placed ...... hmm, where ? In the frame control ??? (it certainly isn't visible anywhere else in an obvious manner)
 
Upvote 0
This worked for me:

Code:
Private Sub Limit_frame_exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim Lvalue As Boolean
    Dim belopp As Currency
    Select Case Limit_frame.ActiveControl.Name
        Case "Long_term_entry"
            Lvalue = IsNumeric(Long_term_entry.Value)
            If Lvalue = True Then
                belopp = Long_term_entry.Value
            End If
            Select Case True
                Case Lvalue = False
                    MsgBox "Invalid value"
                    Cancel = True
                Case Val(belopp) <= 0
                    MsgBox "Muse be >= 0l"
                    Cancel = True
                Case Else
                    ' All okay - get the CURRENT currency and its exchange rate, then calculate
                    ' what the new value in long term belopp should be
                    Call calculate_long_term_belopp_sek(belopp, "")
            End Select
        Case Else
'       Code for other controls here
    End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,767
Messages
6,174,388
Members
452,561
Latest member
amir5104

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