[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.
Re: Run Time error 13 on clearing textboxes
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
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
Re: Run Time error 13 on clearing textboxes
No, none of both solutions works. I get the same error in the "next" instruction.
Re: Run Time error 13 on clearing textboxes
robbedaya, thak works. Thanks a lot! :)
Re: Run Time error 13 on clearing textboxes
Delete your previous code and paste ours.
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.
Re: [RESOLVED] Run Time error 13 on clearing textboxes
Cheers for the explanation. :) We learn new things everyday.
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
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
:wave:
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. :)
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).
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?