We help IT Professionals succeed at work.

Parent and Subform close event

pdvsa asked

I have a Close button on sbfrmEndUser and when I click it is closes the parent but I need it to close the sbfrmEndUser

How can I reference this?  

Parent: frmContacts
Subform:  sbfrmContacts

Thank you,
Watch Question

Chief Technology Officer
You don't close subforms.  Instead, you set the subform control's SourceObject property to an empty string, which drops the subform from the control.
frmContacts.subFormControl.SourceObject =  ""
pdvsaProject finance



Hi Mark, than you for the response.  

I have a follow up. 

The parent form name is actually Contact Details (I incorrectly ref'd as frmContacts. 

I referenced in code as

[Contact Details].subFormControl.SourceObject = ""

but errors and says cant find the field referred to in the expression.  I guess its due to the form name.  Not sure if brackets is necessary or something else. If have a sec please let me know...thank you in advance. 

Mark EdwardsChief Technology Officer
You can refer to a main form that has a subform control on it several ways:

If your code is in the main form, you can use the Me reference:
Me.SubformControlNameHere.SourceObject = ""

If your code is in the subform, use the "Parent" reference:
Parent.SubformControlNameHere.SourceObject = ""

From anywhere you can use the globally available reference:
Forms("MainFormNameHere").SubformControlNameHere.SourceObject = ""

I don't want to insult anyone's intelligence, but experience has shown that sometimes it needs explaining.  When someone gives an example with something like:
Me.SubformControlNameHere.SourceObject = ""
the name "SubformControlNameHere" means to use the name of YOUR subform control - not "SubformControlNameHere" in your code.  If we don't know the actual names, we have to try to get you to insert what you know to be the names in the examples we give.

Hope that helps...
pdvsaProject finance



HI Mark, thanks for the advice!  Its working.  Not quite as intended but its good enough.