PDA

View Full Version : Run-time error 402 for Userform



swaggerbox
11-13-2015, 03:33 AM
I have the userform with a textbox on it. I want to validate for the presence of several characters and phrases (placed them in an array). It is generating an error "Run-time error 402. Must close or hide topmost modal form first". I need to hide userform2 when I display back userform1. Any suggestions how to change the code?



Private Sub CommandButton1_Click()

Dim ArrayCh As Variant

ArrayCh = Array("etc", "<", "%", ">", ";", "[", "]")

For i = LBound(ArrayCh) To UBound(ArrayCh)
If InStr(1, UserForm2.TextBox1.Text, ArrayCh(i), vbTextCompare) > 0 Then
MsgBox "The word/phrase/character " & ArrayCh(i) & " is not allowed in the title"
Else
UserForm1.txtTI1.Text = UserForm2.TextBox1.Text
UserForm2.Hide
End If
Next i
End Sub

SamT
11-13-2015, 08:16 AM
Private Sub CommandButton1_Click()

Dim ArrayCh As Variant

ArrayCh = Array("<", "%", ">", ";", "[", "]")

For i = LBound(ArrayCh) To UBound(ArrayCh)
If InStr(1, Me.TextBox1.Text, ArrayCh(i), vbTextCompare) > 0 Then
MsgBox "The characters <, %, >, ;, [, and ] are not allowed in the title"
Exit Sub
End If
Next i

If InStr(1, LCase(Me.TextBox1.Text), "etc", vbTextCompare) > 0 Then
MsgBox "The word etc is not allowed in the title"
End If

Unload Me
UserForm1.txtTI1.Text = UserForm2.TextBox1.Text
E

snb
11-13-2015, 02:13 PM
Why using a msgbox in a userform ?
The userform is the best tool to inform users.

Why using a second userform if a userform offers multipages, frames, tabstrips, etc. ?

swaggerbox
11-13-2015, 06:11 PM
Wow that was sweet Sam! I will take note of your advice snb

SamT
11-14-2015, 09:42 AM
I'm with snb.

Di you notice the omission in that code?