Compiler Error: User-defined types not defined

后端 未结 3 537
慢半拍i
慢半拍i 2020-11-30 08:35

I get the compile-time error \"User-defined types not defined\" on this line:

Dim cn As ADODB.Connection

What could be wrong?

Code:

相关标签:
3条回答
  • 2020-11-30 09:10

    I had forgotten to add a reference to "Microsoft ActiveX Data Objects 2.5 Library": This reference is required for early binding.

    How to get to that reference:

    Tools > References > Check the checkbox in front of "Microsoft ActiveX Data Objects 2.5 Library"

    Other libraries that work include:

    Microsoft ActiveX Data Objects 2.6 Library

    Microsoft ActiveX Data Objects 2.7 Library

    Microsoft ActiveX Data Objects 2.8 Library

    Microsoft ActiveX Data Objects 6.1 Library

    0 讨论(0)
  • 2020-11-30 09:23

    You can use late binding:

    Dim cn As Object
    

    will make the problem go away. VBA will make the reference automatically when the Set cn = CreateObject("ADODB.Connection") statement is executed.

    0 讨论(0)
  • 2020-11-30 09:23

    I tried adding Microsoft ActiveX Data Objects 2.5 and 2.8 library, but it did not work out. But when I tried creating new object like below it worked.

    Set cn = CreateObject("ADODB.Connection")
    
    0 讨论(0)
提交回复
热议问题