Loop through controls on pages in a multi-tab form

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

Guest

Need to reset controls(CB,TB,OptionGroup) in several of the pages in the
multi-tab form as pseudo code below. But each page in the multi-tab is not
collection. Can someone help me to get arround the problem. Thanks.

ClearText()
For Each ctl In Me![PageC3]
If <controltype is acOptionGroup, acComboBox, acTextBox> Then
ctl.SetFocus
ctl.Text = ctl.DefaultValue
End If
Next
Do the same thing for other pages
 
Are there option groups, combo boxes, textboxes that are on the form but not
on a tab page? If no, just loop through the form's controls and ignore the
tab control:

For Each ctl In Me![PageC3]
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

Note that you do not need to set focus to a control to change its value --
just use the Value property, not the Text.

If yes, I assume that you don't want to affect the controls that are not on
a tab page. So then what you can do is test if the control has a Parent of a
Parent; if it does, then you can test the Name of that Parent and see if
it's the name of the tab control. You'll need to trap for the error that
will occur if the control doesn't have a Parent.Parent property:

Dim strParentName As String
On Error Resume Next
For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
strParentName = ctl.Parent.Parent.Name
If Err.Number = 0 Then
If strParentName = "NameOfTabControl" Then _
ctl.Value = ctl.DefaultValue
ElseIf Err.Number = 2452 Then
' control is on main form
Err.Clear
Else
' some other error has occurred
MsgBox "Error"
Err.Clear
End If
End Select
Next ctl
 
Hi Ken,

Thank you very much for your answer which is full of helpful information.
My case is all controls are on the tab pages.
I tried the first approach. But the ctl loop errors out the FOR statement
with "the object does not support the method or property" without entering
the loop.
I added 'On Error Resume Next' in front, it only goes once, down to the
Select statement and the quickwatch shows ctl="nothing" and Me![Page4] no
value.
(Access2002; 12 pages/MultiTab form; 200 controls)
Would you please give me some more idea on this problem.

Thanks
Li


Ken Snell (MVP) said:
Are there option groups, combo boxes, textboxes that are on the form but not
on a tab page? If no, just loop through the form's controls and ignore the
tab control:

For Each ctl In Me![PageC3]
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

Note that you do not need to set focus to a control to change its value --
just use the Value property, not the Text.

If yes, I assume that you don't want to affect the controls that are not on
a tab page. So then what you can do is test if the control has a Parent of a
Parent; if it does, then you can test the Name of that Parent and see if
it's the name of the tab control. You'll need to trap for the error that
will occur if the control doesn't have a Parent.Parent property:

Dim strParentName As String
On Error Resume Next
For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
strParentName = ctl.Parent.Parent.Name
If Err.Number = 0 Then
If strParentName = "NameOfTabControl" Then _
ctl.Value = ctl.DefaultValue
ElseIf Err.Number = 2452 Then
' control is on main form
Err.Clear
Else
' some other error has occurred
MsgBox "Error"
Err.Clear
End If
End Select
Next ctl


--

Ken Snell
<MS ACCESS MVP>


Li Qiu said:
Need to reset controls(CB,TB,OptionGroup) in several of the pages in the
multi-tab form as pseudo code below. But each page in the multi-tab is not
collection. Can someone help me to get arround the problem. Thanks.

ClearText()
For Each ctl In Me![PageC3]
If <controltype is acOptionGroup, acComboBox, acTextBox> Then
ctl.SetFocus
ctl.Text = ctl.DefaultValue
End If
Next
Do the same thing for other pages
 
Hi Ken,
Thank you very much for your answer which is full of helpful
information. My case is all controls are on the tab pages.
I tried the first approach. But the ctl loop errors out the FOR
statement with "the object does not support the method or property"
without entering the loop.
I added 'On Error Resume Next' in front, it only goes once, down to
the Select statement and the quickwatch shows ctl="nothing" and
Me![Page4] no value.
(Access2002; 12 pages/MultiTab form; 200 controls)
Would you please give me some more idea on this problem.

Thanks
Li


Ken Snell (MVP) said:
Are there option groups, combo boxes, textboxes that are on the form
but not on a tab page? If no, just loop through the form's controls
and ignore the tab control:

For Each ctl In Me![PageC3]
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

Note that you do not need to set focus to a control to change its
value -- just use the Value property, not the Text.

If yes, I assume that you don't want to affect the controls that are
not on a tab page. So then what you can do is test if the control
has a Parent of a Parent; if it does, then you can test the Name of
that Parent and see if it's the name of the tab control. You'll
need to trap for the error that will occur if the control doesn't
have a Parent.Parent property:

Dim strParentName As String
On Error Resume Next
For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
strParentName = ctl.Parent.Parent.Name
If Err.Number = 0 Then
If strParentName = "NameOfTabControl"
Then _ ctl.Value = ctl.DefaultValue
ElseIf Err.Number = 2452 Then
' control is on main form
Err.Clear
Else
' some other error has occurred
MsgBox "Error"
Err.Clear
End If
End Select
Next ctl


--

Ken Snell
<MS ACCESS MVP>


Li Qiu said:
Need to reset controls(CB,TB,OptionGroup) in several of the pages
in the multi-tab form as pseudo code below. But each page in the
multi-tab is not collection. Can someone help me to get arround the
problem. Thanks.

ClearText()
For Each ctl In Me![PageC3]
If <controltype is acOptionGroup, acComboBox, acTextBox>
Then ctl.SetFocus
ctl.Text = ctl.DefaultValue
End If
Next
Do the same thing for other pages

If you refer through the tab control, I think you can do this

dim ctl as control
for each ctl in me.controls("MyTab").Pages("Page4").Controls
select case ctl.controltype
case acOptionGroup, acComboBox, acTextBox
debug.print ctl.name
end select
next ctl

If you know the number of pages, I think you can do

dim ctl as control
dim lngCounter as long
for lngCounter = 0 to 11
for each ctl in me.controls("MyTab").Pages(lngCounter).Controls
select case ctl.controltype
case acOptionGroup, acComboBox, acTextBox
debug.print ctl.name
end select
next ctl
next lngCounter
 
Hi Roy-Vidar,

Thanks a lot for your quick answer. I used the second method to loop
through reset-needed pages which has continous numbers. It is successful.

I appreciated your help very much! Thanks again!
Li Qiu


RoyVidar said:
Hi Ken,

Thank you very much for your answer which is full of helpful
information. My case is all controls are on the tab pages.
I tried the first approach. But the ctl loop errors out the FOR
statement with "the object does not support the method or property"
without entering the loop.
I added 'On Error Resume Next' in front, it only goes once, down to
the Select statement and the quickwatch shows ctl="nothing" and
Me![Page4] no value.
(Access2002; 12 pages/MultiTab form; 200 controls)
Would you please give me some more idea on this problem.

Thanks
Li


Ken Snell (MVP) said:
Are there option groups, combo boxes, textboxes that are on the form
but not on a tab page? If no, just loop through the form's controls
and ignore the tab control:

For Each ctl In Me![PageC3]
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

Note that you do not need to set focus to a control to change its
value -- just use the Value property, not the Text.

If yes, I assume that you don't want to affect the controls that are
not on a tab page. So then what you can do is test if the control
has a Parent of a Parent; if it does, then you can test the Name of
that Parent and see if it's the name of the tab control. You'll
need to trap for the error that will occur if the control doesn't
have a Parent.Parent property:

Dim strParentName As String
On Error Resume Next
For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
strParentName = ctl.Parent.Parent.Name
If Err.Number = 0 Then
If strParentName = "NameOfTabControl"
Then _ ctl.Value = ctl.DefaultValue
ElseIf Err.Number = 2452 Then
' control is on main form
Err.Clear
Else
' some other error has occurred
MsgBox "Error"
Err.Clear
End If
End Select
Next ctl


--

Ken Snell
<MS ACCESS MVP>


Need to reset controls(CB,TB,OptionGroup) in several of the pages
in the multi-tab form as pseudo code below. But each page in the
multi-tab is not collection. Can someone help me to get arround the
problem. Thanks.

ClearText()
For Each ctl In Me![PageC3]
If <controltype is acOptionGroup, acComboBox, acTextBox>
Then ctl.SetFocus
ctl.Text = ctl.DefaultValue
End If
Next
Do the same thing for other pages

If you refer through the tab control, I think you can do this

dim ctl as control
for each ctl in me.controls("MyTab").Pages("Page4").Controls
select case ctl.controltype
case acOptionGroup, acComboBox, acTextBox
debug.print ctl.name
end select
next ctl

If you know the number of pages, I think you can do

dim ctl as control
dim lngCounter as long
for lngCounter = 0 to 11
for each ctl in me.controls("MyTab").Pages(lngCounter).Controls
select case ctl.controltype
case acOptionGroup, acComboBox, acTextBox
debug.print ctl.name
end select
next ctl
next lngCounter
 
Sorry... I meant to change a small part of the code that I copied from your
post, and then didn't do that. Here is corrected code for the first method:

For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

--

Ken Snell
<MS ACCESS MVP>

Li Qiu said:
Hi Ken,

Thank you very much for your answer which is full of helpful information.
My case is all controls are on the tab pages.
I tried the first approach. But the ctl loop errors out the FOR statement
with "the object does not support the method or property" without entering
the loop.
I added 'On Error Resume Next' in front, it only goes once, down to the
Select statement and the quickwatch shows ctl="nothing" and Me![Page4] no
value.
(Access2002; 12 pages/MultiTab form; 200 controls)
Would you please give me some more idea on this problem.

Thanks
Li


Ken Snell (MVP) said:
Are there option groups, combo boxes, textboxes that are on the form but
not
on a tab page? If no, just loop through the form's controls and ignore
the
tab control:

For Each ctl In Me![PageC3]
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

Note that you do not need to set focus to a control to change its
value --
just use the Value property, not the Text.

If yes, I assume that you don't want to affect the controls that are not
on
a tab page. So then what you can do is test if the control has a Parent
of a
Parent; if it does, then you can test the Name of that Parent and see if
it's the name of the tab control. You'll need to trap for the error that
will occur if the control doesn't have a Parent.Parent property:

Dim strParentName As String
On Error Resume Next
For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
strParentName = ctl.Parent.Parent.Name
If Err.Number = 0 Then
If strParentName = "NameOfTabControl" Then _
ctl.Value = ctl.DefaultValue
ElseIf Err.Number = 2452 Then
' control is on main form
Err.Clear
Else
' some other error has occurred
MsgBox "Error"
Err.Clear
End If
End Select
Next ctl


--

Ken Snell
<MS ACCESS MVP>


Li Qiu said:
Need to reset controls(CB,TB,OptionGroup) in several of the pages in
the
multi-tab form as pseudo code below. But each page in the multi-tab is
not
collection. Can someone help me to get arround the problem. Thanks.

ClearText()
For Each ctl In Me![PageC3]
If <controltype is acOptionGroup, acComboBox, acTextBox> Then
ctl.SetFocus
ctl.Text = ctl.DefaultValue
End If
Next
Do the same thing for other pages
 
Hi Ken,
Thanks a lot for providing me so much useful information in the two posts.
I really appreciated it.
Li


Ken Snell (MVP) said:
Sorry... I meant to change a small part of the code that I copied from your
post, and then didn't do that. Here is corrected code for the first method:

For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

--

Ken Snell
<MS ACCESS MVP>

Li Qiu said:
Hi Ken,

Thank you very much for your answer which is full of helpful information.
My case is all controls are on the tab pages.
I tried the first approach. But the ctl loop errors out the FOR statement
with "the object does not support the method or property" without entering
the loop.
I added 'On Error Resume Next' in front, it only goes once, down to the
Select statement and the quickwatch shows ctl="nothing" and Me![Page4] no
value.
(Access2002; 12 pages/MultiTab form; 200 controls)
Would you please give me some more idea on this problem.

Thanks
Li


Ken Snell (MVP) said:
Are there option groups, combo boxes, textboxes that are on the form but
not
on a tab page? If no, just loop through the form's controls and ignore
the
tab control:

For Each ctl In Me![PageC3]
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
ctl.Value = ctl.DefaultValue
End Select
Next ctl

Note that you do not need to set focus to a control to change its
value --
just use the Value property, not the Text.

If yes, I assume that you don't want to affect the controls that are not
on
a tab page. So then what you can do is test if the control has a Parent
of a
Parent; if it does, then you can test the Name of that Parent and see if
it's the name of the tab control. You'll need to trap for the error that
will occur if the control doesn't have a Parent.Parent property:

Dim strParentName As String
On Error Resume Next
For Each ctl In Me.Controls
Select ctl.ControlType
Case acOptionGroup, acComboBox, acTextBox
strParentName = ctl.Parent.Parent.Name
If Err.Number = 0 Then
If strParentName = "NameOfTabControl" Then _
ctl.Value = ctl.DefaultValue
ElseIf Err.Number = 2452 Then
' control is on main form
Err.Clear
Else
' some other error has occurred
MsgBox "Error"
Err.Clear
End If
End Select
Next ctl


--

Ken Snell
<MS ACCESS MVP>


Need to reset controls(CB,TB,OptionGroup) in several of the pages in
the
multi-tab form as pseudo code below. But each page in the multi-tab is
not
collection. Can someone help me to get arround the problem. Thanks.

ClearText()
For Each ctl In Me![PageC3]
If <controltype is acOptionGroup, acComboBox, acTextBox> Then
ctl.SetFocus
ctl.Text = ctl.DefaultValue
End If
Next
Do the same thing for other pages
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top