Matt Hall
Matt Hall

Reputation: 2412

Referencing a subform as variable in a module

I've been using a module to take the name of a form and a control as variables to change a property of a control on a form. For example:

Sub mySub(formName As String, controlName As String)

    Forms(formName).Controls(controlName).ForeColor = Colour.RedDark

End Sub

I'd like to do this for a property on a control on a subform as well, but can't quite figure out the syntax to also refer to the subform as a variable too.

I thought it might be something like:

Forms(mainFormName).Form(subFormName).Controls(controlName).ForeColor = Colour.RedDark

...but this isn't working (Object doesn't support this property or method).

Upvotes: 1

Views: 1230

Answers (1)

HansUp
HansUp

Reputation: 97101

My Access 2010 does not recognize a control property named ForeColour; but ForeColor is valid. (I don't know whether there is a locale issue involved here but my locale is US English.)

Other than that, I think you're trying to access ForeColor through the .Form(subFormName) property. Instead of that, reference the subform control, and from there the target control in its contained subform.

In this working example, Form12 contains a subform control named Child0. The subform control contains a form named fsub2 which in turn contains a text box named txtMemo_field. But note the name of the subform (the form name not the control which contains it) does not appear in this statement:

Forms("Form12").Controls("Child0").Controls("txtMemo_field").ForeColor = vbBlue

Upvotes: 1

Related Questions