Deleting the content of all textboxes in a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

I'm triyng to use a loop to do it, but i get the type mismatch error at the "Next contr" line... :

Dim contr As TextBo
For Each contr In Screen.ActiveForm.Control
contr.SetFocu
contr.Text = "
Next cont

Any idea

thanks a lo
Max
 
1. contr should be declared as a control
2. the code should only be performed where the Control Type
is acTextBox
3. set the value to "" as you then do not need to set focus.

In other words, try

Dim contr As Control
For Each contr In Screen.ActiveForm.Controls
If contr.ControlType = acTextBox Then
contr.Value = ""
End If
Next contr

Hope This Helps
Gerald Stanley MCSD
-----Original Message-----
Hi all,

I'm triyng to use a loop to do it, but i get the type
mismatch error at the "Next contr" line... :(
 
Max said:
Hi all,

I'm triyng to use a loop to do it, but i get the type mismatch error
at the "Next contr" line... :(

Dim contr As TextBox
For Each contr In Screen.ActiveForm.Controls
contr.SetFocus
contr.Text = ""
Next contr

Any idea?

thanks a lot
Max

Usually, not all controls on a form are text boxes. Also, you can clear
a text box by setting its Value property, rather than its Text property,
and if you do it that way you don't need to SetFocus to the control.
(It's only VB text boxes that use primarily the Text property -- for
Access text boxes, it's usually the Value property (the default
property) that you want to work with). Revise your code like this:

Dim contr As Control

For Each contr In Screen.ActiveForm.Controls
If contr.ControlType = acTextBox Then
contr = Null
End If
Next contr
 
Back
Top