Microsoft KB Archive/269471

= ACC97: Error When You Close a Form That Instantiates a Class Module =

Article ID: 269471

Article Last Modified on 1/27/2007

-

APPLIES TO


 * Microsoft Access 97 Standard Edition

-



This article was previously published under Q269471



Advanced: Requires expert coding, interoperability, and multiuser skills.



SYMPTOMS
When you try to close a form that contains code that instantiates a class object, Microsoft Access may stop responding (hang), or you may receive the following error message.

In Microsoft Windows 95 or Microsoft Windows 98:
This program has performed an illegal operation and will be shut down.

If the problem persists, contact the program vendor.

When you click Details (on Microsoft Windows Millennium Edition, press ALT+D), you receive the following message:

MSACCESS caused an invalid page fault in MSACCESS.EXE at 0167:3007fld9.

NOTE: The actual memory address may vary.

In Microsoft Windows NT:
An application error has occurred and an application error log is being generated.

MSACCESS.EXE

Exception: access violation (0xc0000005, address:0x3007f1d9)

In Microsoft Windows 2000
Microsoft Access may quit without any error messages.



CAUSE
This problem occurs when both of the following are true:  WithEvents is being used in the class module to track the form's events.  In the Close or Unload event of the form, an object is being destroyed by the following statement: Set object = Nothing 



RESOLUTION
To work around this behavior, create a command button on the form to destroy the object and to close the form. To do so, follow these steps:  Open the form in Design view.  Add a command button to the form, and then set the OnClick property of the command button to the following event procedure: Set fObj = Nothing DoCmd.Close  Open the form in Form view, and then close the form by clicking the command button. Note that the form closes without error.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Create a new Access database.</li> On the Insert menu, click Class Module.</li>  In the new class module, type or paste the following code: Option Explicit Private WithEvents fForm As Form

Sub Init(pForm As Form) If fForm Is Nothing Then Set fForm = pForm End If End Sub

Private Sub Class_Initialize Debug.Print &quot;Object is initialized&quot; End Sub

Private Sub Class_Terminate Debug.Print &quot;Object is terminated&quot; End Sub </li> Save the class module as Class1, and then close the Visual Basic Editor.</li> On the Insert menu, click Form. In the New Form dialog box, click OK to accept Design view.</li> On the View menu, click Code. A new module for the form opens.</li>  Type or paste the following code in the module: Option Compare Database Option Explicit

Private fObj As New Class1

Private Sub Form_Open(Cancel As Integer) On Error GoTo Err_Form_Open fObj.Init Me Exit_Form_Open: Exit Sub Err_Form_Open: Cancel = True MsgBox Err.Description Resume Exit_Form_Open End Sub

Private Sub Form_Close

Set fObj = Nothing End Sub </li> Save the form, and then view it in Form view.</li> On the File menu, click Close.</li></ol>

Note that either Microsoft Access stops responding, or you see one of the error messages mentioned in the &quot;Symptoms&quot; section.

<div class="references_section">