To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier

Reply Post New Thread
 
Thread Tools Display Modes
Old May 29th, 2007, 09:55 AM   #1
RS_Arm
Hyperactive Member
 
RS_Arm's Avatar
 
Join Date: Mar 07
Location: Planet Earth
Posts: 282
RS_Arm is an unknown quantity at this point (<10)
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.
RS_Arm is offline   Reply With Quote
Old May 29th, 2007, 09:59 AM   #2
Pasvorto
PowerPoster
 
Pasvorto's Avatar
 
Join Date: Oct 02
Location: Minnesota, USA
Posts: 2,785
Pasvorto will become famous soon enough (65+)
Re: Run Time error 13 on clearing textboxes

objTXT.Text = ""
__________________
===================================================
If your question has been answered, mark the thread as [RESOLVED]
Pasvorto is offline   Reply With Quote
Old May 29th, 2007, 10:00 AM   #3
Erroneous
Lively Member
 
Join Date: Mar 07
Posts: 118
Erroneous is on a distinguished road (20+)
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
Erroneous is offline   Reply With Quote
Old May 29th, 2007, 10:02 AM   #4
robbedaya
Fanatic Member
 
robbedaya's Avatar
 
Join Date: Jul 02
Location: Belgium
Posts: 871
robbedaya will become famous soon enough (65+)
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.
robbedaya is offline   Reply With Quote
Old May 29th, 2007, 10:03 AM   #5
RS_Arm
Hyperactive Member
 
RS_Arm's Avatar
 
Join Date: Mar 07
Location: Planet Earth
Posts: 282
RS_Arm is an unknown quantity at this point (<10)
Re: Run Time error 13 on clearing textboxes

No, none of both solutions works. I get the same error in the "next" instruction.
RS_Arm is offline   Reply With Quote
Old May 29th, 2007, 10:05 AM   #6
RS_Arm
Hyperactive Member
 
RS_Arm's Avatar
 
Join Date: Mar 07
Location: Planet Earth
Posts: 282
RS_Arm is an unknown quantity at this point (<10)
Re: Run Time error 13 on clearing textboxes

robbedaya, thak works. Thanks a lot!
RS_Arm is offline   Reply With Quote
Old May 29th, 2007, 10:05 AM   #7
Erroneous
Lively Member
 
Join Date: Mar 07
Posts: 118
Erroneous is on a distinguished road (20+)
Re: Run Time error 13 on clearing textboxes

Delete your previous code and paste ours.
Erroneous is offline   Reply With Quote
Old May 29th, 2007, 10:09 AM   #8
si_the_geek
Super Moderator
 
si_the_geek's Avatar
 
Join Date: Jul 02
Location: Bristol, UK
Posts: 30,093
si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)
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.
si_the_geek is online now   Reply With Quote
Old May 29th, 2007, 10:18 AM   #9
Erroneous
Lively Member
 
Join Date: Mar 07
Posts: 118
Erroneous is on a distinguished road (20+)
Re: [RESOLVED] Run Time error 13 on clearing textboxes

Cheers for the explanation. We learn new things everyday.
Erroneous is offline   Reply With Quote
Old May 29th, 2007, 10:18 AM   #10
robbedaya
Fanatic Member
 
robbedaya's Avatar
 
Join Date: Jul 02
Location: Belgium
Posts: 871
robbedaya will become famous soon enough (65+)
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.
robbedaya is offline   Reply With Quote
Old May 29th, 2007, 10:19 AM   #11
RS_Arm
Hyperactive Member
 
RS_Arm's Avatar
 
Join Date: Mar 07
Location: Planet Earth
Posts: 282
RS_Arm is an unknown quantity at this point (<10)
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

RS_Arm is offline   Reply With Quote
Old May 29th, 2007, 10:21 AM   #12
Erroneous
Lively Member
 
Join Date: Mar 07
Posts: 118
Erroneous is on a distinguished road (20+)
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.
Erroneous is offline   Reply With Quote
Old May 29th, 2007, 10:26 AM   #13
si_the_geek
Super Moderator
 
si_the_geek's Avatar
 
Join Date: Jul 02
Location: Bristol, UK
Posts: 30,093
si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)si_the_geek has a brilliant future (2000+)
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).
si_the_geek is online now   Reply With Quote
Old May 29th, 2007, 10:35 AM   #14
Erroneous
Lively Member
 
Join Date: Mar 07
Posts: 118
Erroneous is on a distinguished road (20+)
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?
Erroneous is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:33 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.