Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each Subform in Form.... 1

Status
Not open for further replies.

DaveTappenden

Programmer
Jan 16, 2002
21
0
0
Can someone please help me with the code needed to do something like:

foreach subform in form
foreach control in subform
control.enabled = true
control.backcolor = white
next
next

This is part of an idea to make all my forms readonly, with an update button that would run the above code and enable all textboxes on the form and subform(s). Some of my forms have up to 3 subforms, and I want reuseable code to find all subforms.

Thanks
 
Try working with this snipit of code from Access Help files.
You will have to use "acSubForm" (or somthing like that) instead of "acTextBox".
Code:
Sub SetTextBoxProperties(frm As Form)
    Dim ctl As Control

    ' Enumerate Controls collection.
    For Each ctl In frm.Controls
        ' Check to see if control is text box.
        If ctl.ControlType = acTextBox Then
            ' Set control properties.
            With ctl
                .SetFocus
                .Enabled = True
                .Height = 400
                .SpecialEffect = 0
            End With
        End If
    Next ctl
End Sub
 
Thanks for your help.
Based on your sample code, I now have the following code, but it fails at ***
I don't know the code to go thru each control on the subform.

Private Sub LockForm(frm As Form, YesOrNo As Boolean)

Dim FrmCtl As Control
Dim SubFrmCtl As Control
' find all subforms on this form
For Each SubFrmCtl In frm.Controls
' check to see if its a subform
If SubFrmCtl.ControlType = acSubform Then
*** For Each FrmCtl In SubFrmCtl.name.Controls
FrmCtl.Locked = YesOrNo
FrmCtl.Enabled = Not YesOrNo
FrmCtl.BackColor = BackgroundColour
' this is a constant
Next
End If
Next

End Sub

Private Sub Form_Current()

Call LockForm(Me, Not NewRecord)

End Sub
 

Try

For Each FrmCtl In SubFrmCtl.Form.Controls

in your *** line
 
Right on NealRP.

Just make sure the control dosen't have focus.
 
Thanks for your help with this.

Finally, can you tell me the correct way to refer to each control within the selected subform?

My line :

FrmCtl.Locked = YesOrNo

doesn't work. I need my code to cycle thru each text box
on the subform and set various properties.

 
In order to access the properties of the control, you have to use .Properties.Item("Locked") = YesOrNo
Here is how you would check to see if your control is a txtBox.

Code:
frmctl.Properties.Item("Locked") = False
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top