title: Access VBA: Error when declaring the type of a Database object: User defined type is not defined
body: <p>In Access 2010 VBA, if I run this sub:</p>
  
  <pre><code>sub test
  
      Dim db
      Dim rst
  
      Set db = CurrentDb()
      Set rst = db.OpenRecordset("select * from mytable")
  
      Debug.Print "db:  " &amp; TypeName(db)
      Debug.Print "rst: " &amp; TypeName(rst)
  
  end sub
  </code></pre>
  
  <p>then it shows (in the "Inmediate" panel):</p>
  
  <pre><code>db:  Database
  rst: Recordset2
  </code></pre>
  
  <p>so it works, and all libraries are installed correctly (ADO for example).</p>
  
  <p>Ok, now I want to declare explicitly the variable types, by using the types that were shown ("Database" and "Recordset2"), so I modify the sub in this way:</p>
  
  <pre><code>sub test
  
      Dim db   as Database        ' explicitly
      Dim rst  as Recordset2      ' explicitly
  
      Set db = CurrentDb()
      Set rst = db.OpenRecordset("select * from mytable")
  
      Debug.Print "db:  " &amp; TypeName(db)
      Debug.Print "rst: " &amp; TypeName(rst)
  
  end sub
  </code></pre>
  
  <p>and when I run it, I get the following error at line "Dim db   as Database":</p>
  
  <pre><code>Compilation error:
  User defined type is not defined
  </code></pre>
  
  <p>So I understand that type "Database" is not defined (!). Why?</p>
  
  <p>Note: I have also tried:</p>
  
  <pre><code>Dim db   as ADO.Database        ' explicitly
  Dim rst  as ADO.Recordset2      ' explicitly
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>Dim db   as ADODB.Database      ' explicitly
  Dim rst  as ADODB.Recordset2    ' explicitly
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>Dim db   as DAO.Database        ' explicitly
  Dim rst  as DAO.Recordset2      ' explicitly
  </code></pre>
  
  <p>and got same error with all of them. How is it possible? Why does it work if I don't declare the type?</p>
  
  <hr>
  
  <p><strong>Edit</strong>: I have just discovered that Access also offers an ADODB.Connection object for the current database, by calling to "CurrentProject.Connection". So I can explicitly declare:</p>
  
  <pre><code>sub test
  
      Dim db As ADODB.Connection
      Set db = CurrentProject.Connection      ' Access gives an ADODB object too!
  
      Dim rst As ADODB.Recordset
      Set rst = New ADODB.Recordset
      rst.Open "select * from mytable", db
  
      Debug.Print "db:  " &amp; TypeName(db)
      Debug.Print "rst: " &amp; TypeName(rst)
  
  end sub
  </code></pre>
  
  <p>that shows: </p>
  
  <pre><code>db:  Connection
  rst: Recordset
  </code></pre>
  
  <p>So I will use it, since ADO is more modern than DAO.</p>