looping thru controls

J

JohnE

Hello. I have a main form that has a tab control on it. On tab1 there are
controls (no subforms). I need to loop thru the all the controls just on
tab1 and no controls on the main form or any of the other tabs. I need to
lock the controls under certain conditions and would prefer to do it
programatically should any other controls be added at a later time. I am at
a loss on how to accomplish this locking the controls. I know a loop process
is the way to go but not able to so I seek assistance from the group.
Thanks.
.... John
 
B

Beetle

I need to loop thru the all the controls just on
tab1 and no controls on the main form or any of the other tabs

Just as a point of information, just because the controls are on
a tab does not mean that they are not "on the main form". You
would still reference them as part of the main forms controls
collection.

To do what you want you could take advantage of the Tag property.
Enter some value, like 1 (or whatever), in the Tag property of the
controls in question, then loop through them like;

Dim ctl As Control

For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox
If ctl.tag = 1 Then ctl.Locked = True
End Select
Next ctl


You can use a similar method to unlock them.
 
J

JohnE

Beetle, I think that is the way to go. I do realize that the controls on
tab1 are still part of the main form controls and that concerned me in the
looping process since it is only the controls on tab1 (not the main form
direct) are the controls I need to lock down.
Thanks.
.... John
 
D

Douglas J. Steele

Look at the Parent property of the control.

For instance, I just created a sample form (Form11) that had a text box
(named txtOnParent) and a tab control (named TabCtl2) on it. (txtOnParent
had a label Label1 associated with it). TabCtl2 had two pages on it (Page3
and Page4). Page3 had one text box (txtOnPage3, with associated label
Label6), while Page4 had one text box (txtOnPage4, with associated label
Label8)

Running this code

Sub ListControls()
Dim frm As Form
Dim ctl As Control

Set frm = Forms!Form11
For Each ctl In frm.Controls
Debug.Print ctl.Name & " (" & ctl.Parent.Name & ")"
Next ctl
Set frm = Nothing

End Sub

produced the following:

ListControls
txtOnParent (Form11)
Label1 (txtOnParent)
TabCtl2 (Form11)
Page3 (TabCtl2)
Page4 (TabCtl2)
txtOnPage3 (Page3)
Label6 (txtOnPage3)
txtOnPage4 (Page4)
Label8 (txtOnPage4)
 
M

Marshall Barton

JohnE said:
Hello. I have a main form that has a tab control on it. On tab1 there are
controls (no subforms). I need to loop thru the all the controls just on
tab1 and no controls on the main form or any of the other tabs. I need to
lock the controls under certain conditions and would prefer to do it
programatically should any other controls be added at a later time. I am at
a loss on how to accomplish this locking the controls. I know a loop process
is the way to go but not able to so I seek assistance from the group.


Each tab control page has its own Controls collection so you
could use something like:

For Each ctl In tabCtl.Pages(0).Controls

If your form has a lot of controls, it can be significantly
faster to loop through the smaller collection instead of
looping through all the controls on the form (including all
the tab pages).

But, unless you plan to never have any labels on the tab
page, you should still use the Tag property to identify the
controls to lock. That's because labels (and several other
types of controls) do not have a Lock property and you would
have to use error trapping to ignore those controls.
 
D

Dirk Goldgar

JohnE said:
Hello. I have a main form that has a tab control on it. On tab1 there
are
controls (no subforms). I need to loop thru the all the controls just on
tab1 and no controls on the main form or any of the other tabs. I need to
lock the controls under certain conditions and would prefer to do it
programatically should any other controls be added at a later time. I am
at
a loss on how to accomplish this locking the controls. I know a loop
process
is the way to go but not able to so I seek assistance from the group.


If you want to confine your loop to just the controls on the first page of
the tab control, you can cycle through that page's Controls collection. For
example:

Dim ctl As Control

For Each ctl In Me!tabMyTabControl.Pages(0).Controls
Debug.Print ctl.Name
Next ctl

Or, referring to the page by name:

For Each ctl In Me!Page1.Controls
Debug.Print ctl.Name
Next ctl
 

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

Top