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.
jdanaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

ProcError:

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

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jdanaAuthor Commented:
Thanks Dale,

It's an elegant solution.

J
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.