ACC2000: Modules.Count Returns Unexpected Results (201298)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q201298
Moderate: Requires basic macro, coding, and interoperability skills.

SYMPTOMS

In Microsoft Access 97, Modules.Count returns the number of currently open Visual Basic for Applications modules; however, in Microsoft Access 2000, Modules.Count returns the number of modules last opened in the Visual Basic Editor, even if you have not opened the Editor in the current session.

CAUSE

In order to open a module in Microsoft Access 2000, you must use the Visual Basic Editor. One function of the Visual Basic Editor is to remember what modules were opened in the previous session and have those modules open and arranged in the same manner the next time that you run the Visual Basic Editor. Consequently, even though the Visual Basic Editor may be closed, Modules.Count may not return 0.

RESOLUTION

If your goal is to see if any modules are currently open, you can instead check to see if the Visual Basic Editor is visible. To demonstrate, follow these steps:
  1. Open the sample database Northwind.mdb.
  2. Create a new form not based on any table or query and add a command button.
  3. Set the OnClick property of the command button to the following event procedure:
    Private Sub Command0_Click()
       MsgBox VBE.MainWindow.Visible
    End Sub
    					
  4. Close the Visual Basic Editor.
  5. Save the new form and view it in Form view.
  6. Click the command button. Note that you get a message box that says "False."
  7. Click OK on the message box and close the form.
  8. On the Tools menu, point to Macro, and then click Visual Basic Editor.
  9. Minimize the Visual Basic Editor (but do not close it).
  10. Open the new form again, and click the command button.

    Note that the message box now says "True."

MORE INFORMATION

Steps to Reproduce Behavior

  1. Open the sample database Northwind.mdb.
  2. Create a new form not based on any table or query and add a command button.
  3. Set the OnClick property of the command button to the following event procedure:
    Sub Command0_Click()
       MsgBox Application.Modules.Count
    End Sub
    					
  4. Save the form.
  5. In the Visual Basic Editor, close the module associated with the form. At this point, no modules are open in the Visual Basic Editor.
  6. From the list of projects, open the Startup and Utility Functions modules.
  7. Close the Visual Basic Editor and quit Microsoft Access.
  8. Open the sample database Northwind.mdb.
  9. Open the form you've been using and click the command button.

    Note that the message box says "3".

    If you repeat these same steps in Microsoft Access 97, the message box would say "0."

Modification Type:MajorLast Reviewed:6/29/2004
Keywords:kbprb KB201298