Microsoft KB Archive/289664

= You receive a &quot;User-defined type not defined&quot; error message when you compile or run VBA code in Access 2002 =

Article ID: 289664

Article Last Modified on 8/12/2004

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q289664



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

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



For a Microsoft Access 97 version of this article, see 182342.

For a Microsoft Access 2000 version of this article, see 202192.



SYMPTOMS
When you compile or run code, you may receive the following error message:

Compile Error: User-defined type not defined.



CAUSE
You are referring to an object in an object library that you do not have referenced.



RESOLUTION
You must add a reference to the object library that contains the object that you are using in code. To do so, follow these steps:
 * 1) Open any module in the database that is generating the error that is described in the &quot;Symptoms&quot; section of this article.
 * 2) On the Debug menu, click Compile . Note the line of code that is highlighted when you receive the error message.
 * 3) On the Tools menu, click References.
 * 4) Click to select the object library that contains the object that is referenced by the line of code that is highlighted.



MORE INFORMATION
NOTE: This section references Microsoft DAO. DAO is not used in Microsoft Access projects (*.adp), but the same concepts regarding missing object libraries apply.

Not having a reference to the Microsoft DAO Object Library is one of the most common reasons for the error that is described in the &quot;Symptoms&quot; section of this article in Access databases (*.mdb). If the reference is missing, you may receive this error on many common DAO commands such as: Dim db as DAO.Database If you receive this error on a common DAO object, you must add the following reference:

Microsoft DAO 3.6 Object Library

Explicitly Declaring DAO and ADO Objects
A common reference conflict occurs when you have references to both the Microsoft DAO Object Library (DAO) and the Microsoft ActiveX Data Objects Library (ADO). This is because these two libraries contain objects with the same name, such as Recordset. To avoid any compile errors in the case where you add another object library reference that uses the same object names, it is good practice to include &quot;DAO.&quot; in the declaration of any DAO objects. Likewise, when declaring ADO objects, include &quot;ADODB.&quot; in the declaration. The following example uses the Recordset object to illustrate this point:

Example 1: DAO Objects
Reference: Microsoft DAO 3.6 Object Library

Declaration: Dim myRecordset as DAO.Recordset

Example 2: ADO Objects
Reference: Microsoft ActiveX Data Objects 2.1 Library

Declaration: Dim myRecordset as ADODB.Recordset

NOTE: By explicitly declaring the object as ADODB or DAO, the position of the checked reference on the references list is not an issue.

Steps to Reproduce the Behavior
  Create a module, and then type or paste the following procedure: Function Test Dim db as DAO.Database End Function  On the Tools menu, click References, and then click to clear the Microsoft DAO Object Library check box in the References dialog box.  To test this function, type the following line in the Immediate window, and then press ENTER: ?Test Note that you receive the error message that is mentioned in the &quot;Symptoms&quot; section of this article. 

