Results 1 to 14 of 14

Thread: [RESOLVED] Run Time error 13 on clearing textboxes

  1. #1

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Resolved [RESOLVED] Run Time error 13 on clearing textboxes

    Hi.
    I'm using this function to clear all my textbox objects in my form

    Code:
    Private Sub Clear_Click()
    Dim objTXT As TextBox
    
    For Each objTXT In Me
        objTXT.Text = vbNullString
    Next objTXT
    
    Set objTXT = Nothing
    End Sub
    After clearing all textboxes, in the end of the for cycle, i get this error:
    Run-time error 13: Type Mismatch.

    What should i do?

    Thanks.

  2. #2
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Run Time error 13 on clearing textboxes

    objTXT.Text = ""
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  3. #3
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Run Time error 13 on clearing textboxes

    Does this work?

    Code:
    Private Sub Command1_Click()
    Dim cTxt As Control
    
    For Each cTxt In Controls
        If TypeOf cTxt Is TextBox Then
            cTxt.Text = ""
        End If
    Next
        
    End Sub

  4. #4
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Run Time error 13 on clearing textboxes

    Code:
    Dim txt As TextBox
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is TextBox Then
                Set txt = ctrl
                txt.Text = vbNullString
            End If
        Next ctrl
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  5. #5

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: Run Time error 13 on clearing textboxes

    No, none of both solutions works. I get the same error in the "next" instruction.

  6. #6

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: Run Time error 13 on clearing textboxes

    robbedaya, thak works. Thanks a lot!

  7. #7
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Run Time error 13 on clearing textboxes

    Delete your previous code and paste ours.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Run Time error 13 on clearing textboxes

    While robbedaya's code works, it has more code than is needed (the txt variable is not required).

    Erroneous's code should work for you - note that the first line (Dim cTxt As Control) is very important here, it is what stops that error from occurring.

    The Controls collection of a form includes all of the controls.. so using a variable that is a specific control type is not valid. The "If TypeOf" line is what ensures you only work with the textboxes.

  9. #9
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: [RESOLVED] Run Time error 13 on clearing textboxes

    Cheers for the explanation. We learn new things everyday.

  10. #10
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: [RESOLVED] Run Time error 13 on clearing textboxes

    Quote Originally Posted by si_the_geek
    While robbedaya's code works, it has more code than is needed (the txt variable is not required).
    I know the txt-variable is not deeded but it's easier in design time to write your code (if other properties need to be changed) when you can use the <CTRL>+<SPACE>-menu. That's the only reason i created an extra variabele.

    Code:
    Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is TextBox Then
                ctrl.Text = vbNullString
            End If
        Next ctrl
    - Use the thread tools to Mark your Thread as Resolved when your question is answered.
    - Please Rate my answers if they where helpful.

  11. #11

    Thread Starter
    Hyperactive Member RS_Arm's Avatar
    Join Date
    Mar 2007
    Location
    Planet Earth
    Posts
    282

    Re: [RESOLVED] Run Time error 13 on clearing textboxes

    Ok, I agree si_the_geek.
    So, here goes the final code (i hope):

    Code:
    Public Sub ClearTBox(Forms As Form)
    Dim tbox As Control
    For Each tbox In Forms.Controls
        If TypeOf tbox Is TextBox Then
            tbox.Text = vbNullString
        End If
    Next tbox
    
    End Sub


  12. #12
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: [RESOLVED] Run Time error 13 on clearing textboxes

    Still you could easily memorize that it's a .Text pertaining to textboxes. While not declaring one variable will save a bit of memory usage.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] Run Time error 13 on clearing textboxes

    True, but if you are performing more than one operation on the control, the intellisense makes it worthwhile (at least while you are initially writing the code).

  14. #14
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: [RESOLVED] Run Time error 13 on clearing textboxes

    Yes si, in some cases. This is what VB is all about. To make your life easier. Is this thread resolved?

Posting Permissions

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



Click Here to Expand Forum to Full Width