Search code examples
ms-accessreferencesubform

MS Access - Creating a generic Sub to set Properties of controls on a Subform


I've got a generic Sub that I can call from any form and it will enable all Text Boxes and Combo Boxes that have the Tag property set to 'EnableForReadOnly'. I'd like to create something similar for Subforms.

    Public Sub EnableReadOnlyControls(theForm)

Dim i As Integer
' Cycle through the form's controls,

For i = 0 To theForm.Count - 1
    If theForm(i).Tag = "EnableForReadOnly" Then
        If TypeOf theForm(i) Is TextBox Then
            theForm(i).Locked = False
            theForm(i).Enabled = True
        ElseIf TypeOf theForm(i) Is ComboBox Then
            theForm(i).Locked = False
            theForm(i).Enabled = True
        End If
    End If
Next i

End Sub

To call this Sub from a form, on the OnOpen event of the form, I type the following and it works great:

EnableReadOnlyControls Me

I'd like to create a similar generic Sub for Subforms but I'm not sure it would be possible to reference the subform in this generic way. Any thoughts would be greatly appreciated.


Solution

  • I'm reposting Andre's comment here as the answer. Thanks Andre!

    "You can simply use the same function. EnableReadOnlyControls Me.SubformControl.Form. Or the call you have, in the subform's Open event."

    Example: The name of the subform control on my main form is frmCounterTopSub so I added this line to my code and it worked like a charm:

    EnableReadOnlyControls Me.frmCounterTopSub.Form