database 访问 VBA:声明数据库对象的类型时出错:未定义用户定义的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21989581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access VBA: Error when declaring the type of a Database object: User defined type is not defined
提问by freesoft
In Access 2010 VBA, if I run this sub:
在 Access 2010 VBA 中,如果我运行这个子:
sub test
Dim db
Dim rst
Set db = CurrentDb()
Set rst = db.OpenRecordset("select * from mytable")
Debug.Print "db: " & TypeName(db)
Debug.Print "rst: " & TypeName(rst)
end sub
then it shows (in the "Inmediate" panel):
然后它显示(在“中间”面板中):
db: Database
rst: Recordset2
so it works, and all libraries are installed correctly (ADO for example).
所以它可以工作,并且所有库都已正确安装(例如 ADO)。
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:
好的,现在我想通过使用显示的类型(“Database”和“Recordset2”)明确声明变量类型,所以我以这种方式修改子:
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: " & TypeName(db)
Debug.Print "rst: " & TypeName(rst)
end sub
and when I run it, I get the following error at line "Dim db as Database":
当我运行它时,在“Dim db as Database”行出现以下错误:
Compilation error:
User defined type is not defined
So I understand that type "Database" is not defined (!). Why?
所以我知道类型“数据库”没有定义(!)。为什么?
Note: I have also tried:
注意:我也试过:
Dim db as ADO.Database ' explicitly
Dim rst as ADO.Recordset2 ' explicitly
and:
和:
Dim db as ADODB.Database ' explicitly
Dim rst as ADODB.Recordset2 ' explicitly
and:
和:
Dim db as DAO.Database ' explicitly
Dim rst as DAO.Recordset2 ' explicitly
and got same error with all of them. How is it possible? Why does it work if I don't declare the type?
并且所有这些都出现了同样的错误。这怎么可能?如果我不声明类型,为什么它会起作用?
Edit: 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:
编辑:我刚刚发现 Access 还通过调用“CurrentProject.Connection”为当前数据库提供了一个 ADODB.Connection 对象。所以我可以明确声明:
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: " & TypeName(db)
Debug.Print "rst: " & TypeName(rst)
end sub
that shows:
这表明:
db: Connection
rst: Recordset
So I will use it, since ADO is more modern than DAO.
所以我会使用它,因为 ADO 比 DAO 更现代。
采纳答案by 4dmonster
Try to check "References" - you will see there is no DAO library. Your first example works because db has Variant type. And assignment
尝试检查“参考” - 您会看到没有 DAO 库。您的第一个示例有效,因为 db 具有 Variant 类型。和分配
Set db = CurrentDb()
puts COM object DAO.Database in db, and later TypeName confirms this is Database. If you want to use
将 COM 对象 DAO.Database 放入 db,稍后 TypeName 确认这是数据库。如果你想使用
Dim db as DAO.Database
You have to Reference appropriate library (Microsoft DAO for example)
您必须参考适当的库(例如 Microsoft DAO)
回答by AnonymousA
You can declare "db" as an object and everything else is the same. i.e. Dim db As Object
您可以将“db”声明为对象,其他一切都相同。即 Dim db 作为对象

