0

Trying to reference a subform's subform control in VBA. I have a main form which, via buttons, opens up a subform. Within that subform are several buttons that open up another subform. Basically a Russian doll scenario with buttons.

The end goal is to have one function being called with the sub-subform's being used as a variable in a function called GetFile. The workaround right now is that I have different versions of the GetFile function in each sub-subform's VBA.

Here's the sub-subform's button click code:

Private Sub cmdButton_Click()
     Dim strForm As String

     strForm = Me.Name
     Call GetFile(strForm)
End Sub

Which then calls this module:

Function GetFile(strForm As String)

     Forms!frmMainMenu!subFrm.Form!subFrm!chkImport.Visible = True

End Function

Currently, I get this error: Runtime Error '2465'.

1 Answer 1

1

Refer to Form and Subform properties and controls

This is your case:

Forms!Mainform!Subform1.Form!Subform2.Form!ControlName.Enabled

so you probably want

Forms!frmMainMenu!subFrm.Form!subFrm.Form!chkImport.Visible = True

although your question is a bit confusing ("open a subform"? A subform is embedded in the parent form).


It may be easier to pass the form object instead of its name as parameter:

Call GetFile(Me)

then it doesn't matter where in the hierarchy the form is, simply use it directly:

Function GetFile(myForm As Form)

     myForm!chkImport.Visible = True

End Function
Sign up to request clarification or add additional context in comments.

3 Comments

Well. You know what they say, easier option is always best. NO MATTER THE CIRCUMSTANCES.
:). But really, thanks! I was so stuck in the box of thinking to pass the form name as a string, that I didn't think of passing it as an object.
Oh. And sorry for being confusing. No excuses. I'm just dumb. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.