Desable "Cancel" event

G

Guest

I’ve been trying to write a routine to prevent empty textboxes. I’m using the
TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) event. But since I
disabled the close button and using a CommandButton instead; if the user
doesn’t make any entry at all, it’ll still trigger the error message when
closing the form. Is there a way around it. Can I cancel the “cancel eventâ€
while closing the form from the CommandButton?

I can’t retain the focus on the control. I also tried the BeforeUpdate event
but didn’t get the expected results. Do you know of any good source of
related documentation?
 
G

Guest

Public bBlockEvents as Boolean

Private Sub Textbox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
if bBlockEvents then exit sub


End sub

Sub CmdExit_Click()
bBlockEvents = True


end Sub

so use a public variable in the userform module to indicate your intention
and exercise control.
 
G

Guest

Hi,

Just adding without ignoring Tom's code:

Private Sub CommandButton1_Click()
If TextBox1 = "" Then TextBox1.SetFocus: Exit Sub
Unload Me
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1 = "" Then Cancel = True
End Sub
 
G

Guest

Once Again Thank you

Tom Ogilvy said:
Public bBlockEvents as Boolean

Private Sub Textbox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
if bBlockEvents then exit sub


End sub

Sub CmdExit_Click()
bBlockEvents = True


end Sub

so use a public variable in the userform module to indicate your intention
and exercise control.
 

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