Dumb Question - Cancel an Exit Event?

P

Patrick A

I have a 3-field form in a Word 2003 dot.

As the user tabs out of each field, I validate the data in that field
using the _Exit event.

Is there a way to prevent the validation code from firing if the user
clicks either a Cancel button or the red X in the corner of the
form?

I've read many posts on this, but found nothing that works for me. I
can't seem to get things to work properly by moving the validation
code to the next control's _Enter event - then truly odd things begin
happening.

Thanks for either guidance or telling me there is no way around this.

Patrick
 
K

Karl E. Peterson

Patrick A formulated on Monday :
I have a 3-field form in a Word 2003 dot.

As the user tabs out of each field, I validate the data in that field
using the _Exit event.

Is there a way to prevent the validation code from firing if the user
clicks either a Cancel button or the red X in the corner of the
form?

I've read many posts on this, but found nothing that works for me. I
can't seem to get things to work properly by moving the validation
code to the next control's _Enter event - then truly odd things begin
happening.

Thanks for either guidance or telling me there is no way around this.

Which fires first, QueryClose or *_Exit? If QueryClose, just set a
flag that tells your validation routine "never mind" (as appropriate).
If Exit, try throwing a DoEvents first, to trigger the QueryClose event
to run, then test the flag.
 
K

Karl E. Peterson

Patrick A expressed precisely :
I have a 3-field form in a Word 2003 dot.

As the user tabs out of each field, I validate the data in that field
using the _Exit event.

Is there a way to prevent the validation code from firing if the user
clicks either a Cancel button or the red X in the corner of the
form?

Here, do the validation where it says "Validation Proceeding". :)


Option Explicit

Private m_UserBail As Boolean

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "TextBox1_Exit"
If Not m_UserBail Then
Debug.Print " Validation Proceeding"
End If
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "TextBox2_Exit"
If Not m_UserBail Then
Debug.Print " Validation Proceeding"
End If
End Sub

Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "TextBox3_Exit"
If Not m_UserBail Then
Debug.Print " Validation Proceeding"
End If
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As
Integer)
Debug.Print "UserForm_QueryClose"
m_UserBail = (CloseMode = vbFormControlMenu)
End Sub

You may want to consider doing it for Windows close, and such, as well?
 
P

Patrick A

Awesome!!!!

Works just great for when the user closes the form, and I removed the
Cancel button to make things simpler.

Thanks very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top