Search code examples
vbams-accessevents

How to avoid sudden stopped in MS Access?


In my access project, I have Navigation form named DepartmentMgtWindow and one of its subform is named DepartmentPanel. In my DepartmentPanel I also have subform/subreport that target a form named PcnsList, where PcnsList is a continuous form. Consider the image below:

enter image description here

In my event (On Open) for DepartmentMgtWindow, I want to hide PcnsList hence the code:

Private Sub Form_Open(Cancel As Integer)
    Form_PcnsList.Visible = False
End Sub

When I run the project, my access suddenly crashes and gets Microsoft Access has stopped working, and if I comment out the code inside the event it works fine.

I also put On Open event in my DepartmentPanel and I am getting the same result.

Any help/suggestions are highly appreciated.


Solution

  • Code fails because form PcnsList is not open as an independent object and therefore is not in active Forms collection.

    Subform path referencing must be through container names, not form names.

    Looks like PcnsList is on a form that is loaded as Navigation Target. Access assigns container that holds target forms a name of NavigationSubform by default. So if the embedded subform container control is named PcnsList and code is behind DepartmentMgtWindow form, try (yes, that is the word Form, do not replace with a form name):
    Me.NavigationSubform.Form.PcnsList.Visible

    I always give container control a name different from object it holds, like ctrPCN, then:
    Me.NavigationSubform.Form.ctrPCN.Visible

    For code behind DepartmentPanel: Me.ctrPCN.Visible = True

    However, advise to set PcnsList subform container as not visible in its design and then code makes visible when needed. This would eliminate problem code from main form.