|
|
#1 |
|
Hyperactive Member
Join Date: Mar 07
Location: Planet Earth
Posts: 282
![]() |
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
Run-time error 13: Type Mismatch. What should i do? Thanks. |
|
|
|
|
|
#2 |
|
PowerPoster
Join Date: Oct 02
Location: Minnesota, USA
Posts: 2,737
![]() |
Re: Run Time error 13 on clearing textboxes
objTXT.Text = ""
__________________
=================================================== If your question has been answered, mark the thread as [RESOLVED] |
|
|
|
|
|
#3 |
|
Lively Member
Join Date: Mar 07
Posts: 104
![]() |
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 |
|
Fanatic Member
Join Date: Jul 02
Location: Belgium
Posts: 871
![]() |
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 |
|
Hyperactive Member
Join Date: Mar 07
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 |
|
Hyperactive Member
Join Date: Mar 07
Location: Planet Earth
Posts: 282
![]() |
Re: Run Time error 13 on clearing textboxes
robbedaya, thak works. Thanks a lot!
|
|
|
|
|
|
#7 |
|
Lively Member
Join Date: Mar 07
Posts: 104
![]() |
Re: Run Time error 13 on clearing textboxes
Delete your previous code and paste ours.
|
|
|
|
|
|
#8 |
|
Super Moderator
Join Date: Jul 02
Location: Bristol, UK
Posts: 27,113
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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.
__________________
Classic VB FAQs (updated Jun 6th) ...Database Development FAQs/Tutorials (updated Mar 26th) (includes fixing common VB errors) .......... (includes fixing common DB related errors, and [Classic VB] ADO tutorial /further steps, and [VB.Net] ADO.Net Tutorial). Tutorial: How to automate Excel from VB6 (or VB5/VBA) .•. SQL 'Select' statement formatter/checker .•. Convert colour number to colour name .•. FlexGrid: fill from recordset .•. FlexGrid: AutoSize columns .•. DB Reserved Words checker Connection strings .•. MDAC/Jet/ACE downloads .•. SQL Server downloads .•. MZTools (free upgrade for the VB6/VBA Editor) |
|
|
|
|
|
#9 |
|
Lively Member
Join Date: Mar 07
Posts: 104
![]() |
Re: [RESOLVED] Run Time error 13 on clearing textboxes
Cheers for the explanation.
We learn new things everyday.
|
|
|
|
|
|
#10 | |
|
Fanatic Member
Join Date: Jul 02
Location: Belgium
Posts: 871
![]() |
Re: [RESOLVED] Run Time error 13 on clearing textboxes
Quote:
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 |
|
Hyperactive Member
Join Date: Mar 07
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 |
|
Lively Member
Join Date: Mar 07
Posts: 104
![]() |
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 |
|
Super Moderator
Join Date: Jul 02
Location: Bristol, UK
Posts: 27,113
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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).
__________________
Classic VB FAQs (updated Jun 6th) ...Database Development FAQs/Tutorials (updated Mar 26th) (includes fixing common VB errors) .......... (includes fixing common DB related errors, and [Classic VB] ADO tutorial /further steps, and [VB.Net] ADO.Net Tutorial). Tutorial: How to automate Excel from VB6 (or VB5/VBA) .•. SQL 'Select' statement formatter/checker .•. Convert colour number to colour name .•. FlexGrid: fill from recordset .•. FlexGrid: AutoSize columns .•. DB Reserved Words checker Connection strings .•. MDAC/Jet/ACE downloads .•. SQL Server downloads .•. MZTools (free upgrade for the VB6/VBA Editor) |
|
|
|
|
|
#14 |
|
Lively Member
Join Date: Mar 07
Posts: 104
![]() |
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?
|
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|