Consulting

Results 1 to 5 of 5

Thread: Run-time error 402 for Userform

  1. #1

    Run-time error 402 for Userform

    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

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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. ?

  4. #4
    Wow that was sweet Sam! I will take note of your advice snb

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I'm with snb.

    Di you notice the omission in that code?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •