loop thru controls

  • Thread starter Thread starter sam
  • Start date Start date
S

sam

I posted quite a while ago but it's dissapeared so....

I'm wanting to loop thru all the controls on an unbound
form and and clear any data that may be contained in a
control (clear the form) but am having trouble getting it
to work.

Dim ctl As Control
For Each ctl In Me.Controls

ctl.something=something *** can't get this right***
Next ctl

Any ideas?
 
sam said:
I posted quite a while ago but it's dissapeared so....

I'm wanting to loop thru all the controls on an unbound
form and and clear any data that may be contained in a
control (clear the form) but am having trouble getting it
to work.

Dim ctl As Control
For Each ctl In Me.Controls

ctl.something=something *** can't get this right***
Next ctl

Any ideas?

Dim ctl as Control

For Each Ctl in Me
Ctl = Null
Next Ctl

Here are the problems though. This will also cycle through controls that have no
value property (labels, lines, boxes, etc.). This will cause an error in your loop.
You need to do one of the following.

Test the Control Type before assigning the Null value.
or
Use "On Error Resume Next" and take your chances.
or
Use an Error Handler and when the error thrown is from the above condition then
Resume Next.
or
Apply a common Tag property to only the controls you want to clear and then test for
that.

The latter is what I usually go with as it also allows me to leave some controls out
of the loop if I need to.

For Each Ctl in Me
If Ctl.Tag = "Clear" Then Ctl = Null
Next Ctl
 
You need to know what type of control it is. You
say "Clear" the controls, most

controls can't be cleared. If the controls in question
are text boxes, the following

does this. For other controls, insert code as appropriate
in the select

Private Sub Command14_Click()
Dim ctlLoop As Control
For Each ctlLoop In Me.Controls
Select Case ctlLoop.Properties("ControlType")
Case acCheckBox
Case acComboBox
Case acCommandButton
Case acCustomControl
Case acImage
Case acLabel
Case acLine
Case acListBox
Case acObjectFrame
Case acOptionButton
Case acOptionGroup
Case acPage
Case acPageBreak
Case acRectangle
Case acSubform
Case acTabCtl
Case acTextBox
ctlLoop = vbNullString
Case acToggleButton
End Select
Next
End Sub
 
Back
Top