Losing Form Object Reference in Module-Level Variable

S

Steve

Hello -

I am hoping someone can enlighten me to why this either a) Access 97 bug, or
b) my ignorance, is causing the following results:


Initial Problem:
I have a subform which needs to reference the parent form in multiple
locations in multiple procedures. Originally I just used plain old
Me.Parent, which worked fine except the database after closing would hang
Access (which would never close short of rebooting or forcing it with the
Task Manager).


Initial Workaround:
Through careful elimination, I found that the Access hang only occurred
after using a procedure that had the Me.Parent reference, and further
elimination narrowed it down to those lines. I found a workaround by
creating a form object that I set to the parent form:

Dim frmParent as Form
Set frmParent = Me.Parent


My Current Issue:
This worked in that procedure, so I proceded to create a module-level
variable to store the reference once, setting it in the form's Open event:

(General Declarations)
Private mfrmParent As Form

(Form_Open)
Set mfrmParent = Me.Parent

(Form_Close)
Set mfrmParent = Nothing

Then I used the mfrmParent form variable - which should point to the
unchanging parent form from this subform's opening until closing - in every
procedure in place of Me.Parent. No hang, but now I receive an error of:
Run-time error '91':
Object variable or With block variable not set

This error occurs even if it is the first statement in the procedure like
this:
Debug.Print mfrmParent.Name

That same statement would work in the Form_Open event, but everywhere else I
receive this error. Considering that I am using a module scope variable, how
is it losing its reference? There are no other Sets of that variable
anywhere (I even removed the Form_Close procedure's Set mfrmParent = Nothing
to be sure), and the form & subform never even lose focus. Any help
understanding this situation would be much appreciated!

Thanks!
Steve
 
A

Allen Browne

Steve, this is very interesting.

Access does open the subform before the parent. I don't know if this may
impact the fact that the reference to the Parent form goes out of scope
again immediately after it is set in the subform's Open event. You could try
moving the initialization of the variable into the subform's Load event
instead, and see if this helps.


I have used Me.Parent in-line without triggering the "Access won't close"
problem, so there could be some further factors specific to what you are
doing that influences the triggering of this bug. It might be interesting to
track down what those were.

Two other factors that can trigger this bug are:
1. Failing to close objects you opened - particularly Recordsets - and set
your objects to Nothing.

2. Referring to boolean controls (check box, toggle button, option button)
like this:
If Me.MyCheckBox Then

Another factor that can trigger Error 91 is closing the default workspace
(typically after working with transactions).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top