Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Error When You Close a Form That Instantiates a Class Module


View products that this article applies to.

This article was previously published under Q223245
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

↑ Back to the top


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:
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 015f:300581e7.
NOTE: The actual memory address may vary.

↑ Back to the top


Cause

This problem occurs when both of the following are true:
You use WithEvents in the Class module to track the form's events.
In the Close or Unload event of the form, you destroy an object with the following statement:
Set object = Nothing
					

↑ Back to the top


Resolution

To avoid receiving the error message mentioned in the "Symptoms" section of this article, create a custom command button on the form to destroy the object and to close the form. To do so, follow these steps:
1.Open the form in Design View.
2.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
					
3.Open the form in Form view, and then close the form with the command button. Note that the form closes without error.

↑ Back to the top


Status

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

↑ Back to the top


More information

Steps to Reproduce the Behavior

1. Create a new Access database.
2.In the Database window, click Modules under Objects.
3.On the Insert menu, click Class Module. A new module opens.
4.In the new class module, type 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 "Object is initialized"
End Sub

Private Sub Class_Terminate()
    Debug.Print "Object is terminated"
End Sub
					
5. Save the class module as Class1 and close the Visual Basic Editor.
6.In the Database window, click Forms under Objects, and then click Create form in Design View.
7.On the View menu, click Code. A new module opens.
8. Type the following 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
					
9. Save the form and view it in Form view.
10. On the File menu, click Close.
Note that either Microsoft Access stops responding, or you see the error message mentioned in the "Symptoms" section.

↑ Back to the top


References

For more information about class modules, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type class modules in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB223245, kbpending, kbbug, kberrmsg

↑ Back to the top

Article Info
Article ID : 223245
Revision : 2
Created on : 6/24/2004
Published on : 6/24/2004
Exists online : False
Views : 337