• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1558
  • Last Modified:

MS Access - VBA that determines if form is opening in sub-form environment

I have a form that we'll call frmParent for the sake of simplicity that has three sub-forms, each of which includes VBA which inspects fields on frmParent. The code under each sub-form works fine when they're are opened as sub-forms to frmParent, but when I run them as stand-alone forms (for debugging purposes only), the VBA throws an error. It's not a big deal, the end users aren't going to see the error, but it would be kind of nice to splice in a little piece of VBA on each sub-form that stipulates, "If you don't open as a sub-form to frmParent, then don't call the following code that looks for values on frmParent."

How do I pull this off? In other words, I need some VBA that determines if frmParent is currently present as a parent form or not present as a parent form. Alternatively, I need some code that recognizes if a form has opened in a stand-alone environment or a sub-form environment.
1 Solution
Dale FyeCommented:
I would simply add an error handler and if the error that is called references the parent then ignore the code that follows, something like:
Private sub Form_Load

    On Error goto ProcError

    'this will raise an error if 
    if me.parent is nothing then Exit Sub

    'your other code goes here

    On Error Resume Next
    'code here to cleanup any objects created or opened in the procedure
    Exit Sub


    if err.number = 2452 then
        Resume ProcExit
        msgbox err.number & vbcrlf & err.description, , "Form Name Load Event"
        Resume ProcExit

End Sub

Open in new window

I usually end my error handler by resuming at the ProcExit row so that I can cleanup any objects that were created during the procedure.
jdanaAuthor Commented:
Thanks Dale,

It's an elegant solution.


Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now