Microsoft KB Archive/167222

From BetaArchive Wiki

Article ID: 167222

Article Last Modified on 1/19/2007



APPLIES TO

  • Microsoft Access 97 Standard Edition



This article was previously published under Q167222

Moderate: Requires basic macro, coding, and interoperability skills.


SYMPTOMS

When you run your code in Microsoft Access 97 after converting a database from Microsoft Access 7.0, you may receive the following error message:

Compile error: User-defined type not defined.

CAUSE

You are referring to a code module of a form or report using the following syntax in code, and the HasModule property of that form or report is set to No:

Dim f as Form_Customers
                

-or-


Dim f as New Form_Customers
                

RESOLUTION

To resolve this issue, use one of the following methods:

  1. Manually set the HasModule property of the form(s) and/or report(s) you are referencing to Yes.
  2. Use the following syntax instead:

    Dim f as Form
    
    Set f = Forms!Customers
                            

    NOTE: Before the Set statement, you must programmatically check to see if the form is open. If it isn't, first open the form using the OpenForm method.

  3. Use the following code to programmatically set the HasModule property of all forms and reports to Yes.

    NOTE: This will make all forms and reports non-lightweight objects, and therefore may make them load more slowly. If you know you do not need to reference all forms or reports with the above syntax, you may want to go back through and manually change the HasModule property to No for those objects.

          Option Compare Database
          Option Explicit
    
          Sub SetHasModuleTrue()
    
             On Error GoTo Err_SetHasModuleTrue
    
             Dim MyDb As Database
             Dim MyDoc As Document
             Dim bolObjectState As Boolean
             Dim strMessage As String
    
             Set MyDb = CurrentDb  ' Set the current dabatase.
    
             ' Run this section to set HasModule property to Yes for
             ' all the forms that don't already have HasModule set to Yes.
    
             ' Go through each form in the database.
             For Each MyDoc In MyDb.Containers("Forms").Documents
                ' Get the state of the form.
                bolObjectState = SysCmd(acSysCmdGetObjectState, acForm, _
                   MyDoc.Name)
                If bolObjectState = 0 Then  ' If the form is not open,
                   ' Open the form in design view.
                   DoCmd.OpenForm MyDoc.Name, acDesign, , , , acHidden
                   ' If the HasModule Property is No,
                   If Not Forms(MyDoc.Name).HasModule Then
                      Forms(MyDoc.Name).HasModule = True  ' set HasModule to
                                                          ' Yes.
    
                   DoCmd.Save acForm, MyDoc.Name ' Save the form.
                   End If
                   DoCmd.Close acForm, MyDoc.Name  ' Close the form.
                Else
                   ' If the form is open but not in Design view,
                   If Forms(MyDoc.Name).CurrentView = 1 Then
                      ' change the form to Design view.
                      DoCmd.OpenForm MyDoc.Name, acDesign
                   End If
                   ' If the HasModule Property is No,
                   If Not Forms(MyDoc.Name).HasModule Then
                      Forms(MyDoc.Name).HasModule = True ' set HasModule to Yes
                      strMessage = "Form '" & MyDoc.Name & "' is already open."
                      strMessage = strMessage & " The HasModule Propterty has"
                      strMessage = strMessage & " been set to Yes. Would you"
                      strMessage = strMessage & " like to save and close the"
                      strMessage = strMessage & " form now?"
                      ' Ask users if they want to save and close a previously
                      ' opened form. If yes
                      If MsgBox(strMessage, vbYesNo + vbInformation) = _
                            vbYes Then
                         DoCmd.Save acForm, MyDoc.Name  ' save the form.
                         DoCmd.Close acForm, MyDoc.Name  'Close the form.
                      End If
                   End If
                End If
             Next MyDoc ' Go to next form.
    
             ' Run this section to set HasModule property to Yes for
             ' all the Reports that don't already have HasModule set to Yes.
             DoCmd.Echo False
             ' Go through each report in the database.
             For Each MyDoc In MyDb.Containers("Reports").Documents
                ' Get the state of the report.
                bolObjectState = SysCmd(acSysCmdGetObjectState, acReport, _
                    MyDoc.Name)
                If bolObjectState = 0 Then  'If the report is not open
                   ' Open the report in Design view.
                   DoCmd.OpenReport MyDoc.Name, acDesign
                   ' If the HasModule Property is No,
                   If Not Reports(MyDoc.Name).HasModule Then
                      ' set the HasModule to Yes.
                      Reports(MyDoc.Name).HasModule = True
                      DoCmd.Save acReport, MyDoc.Name  ' Save the report.
                   End If
                   DoCmd.Close acReport, MyDoc.Name  ' Close the report.
                Else
                   ' If the report is already open, open it in Design view.
                   DoCmd.OpenReport MyDoc.Name, acDesign
                   ' If the HasModule Property is No,
                   If Not Reports(MyDoc.Name).HasModule Then
                      ' set HasModule to Yes.
                      Reports(MyDoc.Name).HasModule = True
                      strMessage = "Form '" & MyDoc.Name & "' is already open."
                      strMessage = strMessage & " The HasModule Propterty has"
                      strMessage = strMessage & " been set to Yes. Would you"
                      strMessage = strMessage & " like to save and close the"
                      strMessage = strMessage & " report now?"
                      ' Ask users if they want to save and close a previously
                      ' opened report. If yes
                      If MsgBox(strMessage, vbYesNo + vbInformation) = _
                            vbYes Then
                         DoCmd.Save acReport, MyDoc.Name  ' save the report.
                         DoCmd.Close acReport, MyDoc.Name  'Close the report.
                      End If
                   End If
                End If
             Next MyDoc ' Go to the next report.
             DoCmd.Echo True
          Exit_SetHasModuleTrue:
             Exit Sub
          Err_SetHasModuleTrue:
             DoCmd.Echo True
             MsgBox Err.Description
             Resume Exit_SetHasModuleTrue
          End Sub
                        


MORE INFORMATION

When you convert a Microsoft Access 7.0 database to Microsoft Access 97, only forms or reports that contain code will have their HasModule property set to Yes.

When you refer to a new instance of a form or report, you are actually creating a new instance of that object's class module. In order to be able to do that, the form or report has to have a class module to refer to. In Microsoft Access 97 forms and reports do not have class modules if the HasModule property is set to No. In Microsoft Access 7.0, a form or report always has a class module.

Steps to Reproduce Behavior

  1. Open the sample database Northwind.mdb.
  2. Create a module and type the following procedure:

          Function TestNewInstance ()
             Dim f as Form_Customers
          End Function
                        
  3. To test this function, type the following line in the Debug window, and then press ENTER.

    ?TestNewInstance()

    Note that you receive the error message mentioned in the "Symptoms" section.


REFERENCES

For more information about referencing forms or reports as described in this article, search the Help Index for "multiple instances of form," or ask the Microsoft Access 97 Office Assistant.


Additional query words: predefined

Keywords: kberrmsg kbprb kbprogramming KB167222