Why does exit event triggers when control is clicked?

ShieBoon

Board Regular
Joined
May 3, 2011
Messages
111
Hi all, I have a userform, with textboxes within to accept input from users. In the exit events of these textboxes, i do data validation.

When the data entered by user is not valid, i prompt a messagebox and set the cancel property of the exit event to true.

As i test my application, i play around with the controls.
Thing is, i am puzzled why the exit event is triggered even when the control is clicked, and this cause the messagebox to appear 2 times. It is pretty irritating and i wish to get rid of it.

Could anybody kindly explain to me what exactly triggers the exit event?

Any ideas?

Thanks alot

Excel 2003
Shie Boon
 
I explained in my post that I have not exited the field with the exit event code. I exited another field that has no exit event code.

"This exit event gets triggered anytime I click in the other fields within the 3rd frame, even if I am not exiting the field with the exit event (for example, the first field in the first frame is selected; I click on the 2nd field in the 3rd frame and the exit event for the 1st field in the 3rd frame fires).
If I am selecting a textbox by clicking in it, and the combobox with the exit event code is not the current focus, then the exit event should not be triggered. In my case it is.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Maybe it's my lack of skills, but I punted on frames in favor of labels the last time I did a complex (to me) form. I had problems I just couldn't sort out, and things went smoothly thereafter.

IIRC, the Exit event of a control is actually an event of the container object, which is the frame if there is one (Rory?).
 
Upvote 0
First I must apologise - it appears that when we ran out of coffee at work yesterday, my brain stopped working and I lost the ability to read or think. (if you have an 8 week old baby, you know how important coffee is). I will now attempt to remove my dunce's cap and replace it with my pedagogue panama...

So, frames and forms. What fun. In a sense you can think of a frame as a subform, since it does indeed act as a container for the controls within it, and effectively that means that the exit events only apply within that frame. This is why the tabindex for controls within the frame starts again at 0.

A few examples:

1. Assume a form with two textboxes and exit events for each. Run the form and click or tab between controls, and the exit events will fire.
2. Now put a frame around the first textbox. Clicking between the two textboxes will now only fire the exit event for the second textbox (the one not in a frame).
3. Now put a frame around the second textbox too. Now no exit events will fire as you switch between the controls.

NOTE: for both 2 and 3 above, the exit events for the textboxes will subsequently fire when the form is unloaded.

4. Now add 2 more textboxes to one of the frames. (let's say you have frame1 with tb1, and frame 2 with tb2, tb3 and tb4)

i. Moving from tb1 to tb2 - switching frames - does not trigger tb1_exit.
ii. From tb2 to tb3 triggers tb2_exit.
iii. from tb3 to tb4 triggers tb3_exit.
iv. From tb4 to tb1 - switching frames again - does not trigger tb4_exit.
v. if you then move from tb1 to tb2, you will then see the tb4_exit event trigger, since you have now switched control within that frame.

However, if at step v. you click from tb1 to tb3, you will see tb4_exit fire and then tb2_exit (assuming tb2 is tabindex 0 in the frame order).

So it appears that whichever control in the frame is first in the tab order for that frame, will always get the focus when you click into the frame. If you actually clicked a different control, focus will subsequently shift to that control.

Hopefully that makes sense, but I did only get 20 minutes sleep last night.

PS All of the above assumes that you have the frames set to cycle all forms.
 
Upvote 0
This is good to know and explains why I have had so many issues with this complex form. The frames were causing all of my issues. Like the previous poster, I have removed the frames in favor of text boxes that look like frames. The text boxes are a bit of a pain to administer but at least the code works. It would be nice if there was an easier way, like a property for the frame that gives the option to just use it for looks with no functionality. Thanks everyone for the responses. I love this site!!!
 
Upvote 0
When validating data from a control I use either BeforeUpdate (if you want to catch before it's committed) or Afterupdate events on the control itself. Do this hwen you are validating *that control*.

To validate a record (or contents in one control relative to another control) I use the BeforeUpdate event of the *form*. This is possibly more like what you need.

In air code:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
  If IsNull(MyDate) Then
    MsgBox "You must enter a valid date into MyDate",vbCritical
    Cancel = True
    MyDate.SetFocus
  Else
    'Other tests if required, or else just save the record
  End If
End Sub

Maybe I should add: I spend a lot of time in Access, and that is how I handle validation in Access.

Denis
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,721
Members
449,465
Latest member
TAKLAM

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