G
Greg
I am trying figure out how to "terminate" a userform and code in
progress if a user clicks a "Cancel" command button or the "X" on the
form. Here is the problem. The user enters text in a textbox that
could result in an error. That text is passed as a variable to another
sub as part of a Do Loop. If the error is generated, the user is
notified with a msgbox and the form is displayed with the invalid entry
highlighted. At this point, I want the user to be able to cancel the
process and eliminate any additional UserFrom code from running. I
mean I want to kill it until it stops breathing completely.
I can't get it worked out. I have duplicated the problem in the simple
scenario provided below. All it takes is a user form, 1 text box, and
2 command buttons. Enter the value "2" in the text box and click the
command button 1. When the error is generated and you are returned to
the form then click the cancel button. This is where I want to strike
the death blow, but as you will see the code continues to running
(bypassing oddly enough the call) until the count of 10 is reached.
All help welcome. Thanks.
Option Explicit
Private Sub CommandButton1_Click()
Me.Hide
Dim i&
For i = 1 To 10
CallMsg i
If i > 2 Then MsgBox "Won't stop"
Next i
Exit Sub
End Sub
Sub CallMsg(ByVal i&)
MsgBox i
If i = Me.TextBox1.Text Then
On Error GoTo Handler
Err.Raise 5625
End If
Exit Sub
Handler:
MsgBox ("You must change the test in text box 1 to a number > 10")
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
Me.Show
Err.Clear
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
progress if a user clicks a "Cancel" command button or the "X" on the
form. Here is the problem. The user enters text in a textbox that
could result in an error. That text is passed as a variable to another
sub as part of a Do Loop. If the error is generated, the user is
notified with a msgbox and the form is displayed with the invalid entry
highlighted. At this point, I want the user to be able to cancel the
process and eliminate any additional UserFrom code from running. I
mean I want to kill it until it stops breathing completely.
I can't get it worked out. I have duplicated the problem in the simple
scenario provided below. All it takes is a user form, 1 text box, and
2 command buttons. Enter the value "2" in the text box and click the
command button 1. When the error is generated and you are returned to
the form then click the cancel button. This is where I want to strike
the death blow, but as you will see the code continues to running
(bypassing oddly enough the call) until the count of 10 is reached.
All help welcome. Thanks.
Option Explicit
Private Sub CommandButton1_Click()
Me.Hide
Dim i&
For i = 1 To 10
CallMsg i
If i > 2 Then MsgBox "Won't stop"
Next i
Exit Sub
End Sub
Sub CallMsg(ByVal i&)
MsgBox i
If i = Me.TextBox1.Text Then
On Error GoTo Handler
Err.Raise 5625
End If
Exit Sub
Handler:
MsgBox ("You must change the test in text box 1 to a number > 10")
With Me.TextBox1
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
Me.Show
Err.Clear
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub