Chuck0185
Chuck0185

Reputation: 531

How to loop through all subforms MS Access VBA

I have a main form with many subforms (and sub-Subforms). I would like to have a button on my main subform that will toggle each subform from enabled=true to enabled=false. Is it possible to do this without having to specifically refer to each subform? Maybe something like: For each subform of frmMainForm...?

Upvotes: 3

Views: 2459

Answers (3)

HackSlash
HackSlash

Reputation: 5811

To toggle a boolean setting you set it to Not itself. I also give an alternate way to check the type of an object that works for all objects and not just a control that may or may not have a ControlType property.

For Each Control In Me.Controls
    If TypeOf Control Is SubForm Then
        Control.Visible = Not Control.Visible
    End If
Next

Upvotes: 0

user1598478
user1598478

Reputation: 1

For Each Control In Me.Controls
    If Control.ControlType = acSubform Then
        If Control.Name = sSubform Then
            Control.Visible = True
        Else
            Control.Visible = False
        End If
    End If
Next

help4access.com uses this method to turn subforms on/or when many housed with a single main form.

Upvotes: 0

Gustav
Gustav

Reputation: 56006

Yes, you can loop the controls:

For Each Control In Me.Controls
    If Control.ControlType = acSubform Then
        ' Do something with this subform control.
    End If
Next

Upvotes: 6

Related Questions