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

Posted on 2014-07-13
Last Modified: 2014-07-13
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.
Question by:jdana
    LVL 47

    Accepted Solution

    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.

    Author Closing Comment

    Thanks Dale,

    It's an elegant solution.


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    using Access 8 39
    Access query that references subform 5 23
    Open Args 16 24
    VB6 - Compare and highlight cell not the same 3 14
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now