tang_s
tang_s

Reputation: 97

Calling a second form's subform from the first form in VBA

I'm trying to call a second form's subform from a first form but I'm not sure if this is possible? my code is as followed:

            '2 get the corresponding section
        sectionInfo = GetSectionInfo(optPermit, arr)
            source = sectionInfo(0)
            heightNum = sectionInfo(1)
            topNum = sectionInfo(2)

            rootControl = "subfrmSection" + arr
            With Forms!frmSecondForm!rootControl.Form
                .SourceObject = "subfrmSection" + sectionInfo(0)
                .Top = currentLocation + sectionInfo(1) 'currentLocation + height of object
                .Left = leftNum
                .Height = cmdInfo(2)
                .Width = widthNum
            End With

I'm trying to edit the second form's subform on a button click but I keep getting the Error 2450 stating that it can't find this specific subform. Is there a different syntax for calling a subform in this situation?

Any assistance would be greatly appreciated. Thank you!

EDIT: Thanks to the help of pteranodon, the final code now looks like this (for anyone who many need it):

'2 get the corresponding section
        sectionInfo = GetSectionInfo(optPermit, arr)
        rootControl = "subfrmSection" + arr
        Form_frmSecondForm.Controls(rootControl).SourceObject = "subfrmSection" + sectionInfo(0)
        Form_frmSecondForm.Controls(rootControl).Top = currentLocation + sectionInfo(1) 'currentLocation + height of object
        Form_frmSecondForm.Controls(rootControl).Left = leftNum
        Form_frmSecondForm.Controls(rootControl).Height = sectionInfo(1)
        Form_frmSecondForm.Controls(rootControl).Width = widthNum

Upvotes: 1

Views: 99

Answers (1)

pteranodon
pteranodon

Reputation: 2059

That ! notation is looking for a control literally named rootControl, not a control whose name is held inside the variable rootControl.

Try: With Forms!frmSecondForm.Controls(rootControl).Form

I can't tell if you have the same problem with frmSecondForm. If that is a variable and not the literal name, try: With Forms(frmSecondForm).Controls(rootControl).Form

Upvotes: 2

Related Questions