变量在与 DAO 一起使用之前未声明

发布于 2024-12-25 22:52:29 字数 376 浏览 3 评论 0原文

我正在使用 DAO(被要求不要使用 ADO.NET)来更新 Access 数据库。然而,到目前为止,VB2008 告诉我变量“daoengine”在使用之前没有声明。我在下面的代码中做错了什么?

Function update_db()
    Dim daoengine As DAO.DBEngine
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    dbs = daoengine.OpenDatabase("Project.mdb")
    rst = dbs.OpenRecordset("Log", dao.RecordsetTypeEnum.dbOpenDynaset)

End Function

I'm using DAO (been asked not to use ADO.NET) to update an Access database. I'm currently thus far, however, VB2008 is telling me that the variable "daoengine" is not declared before it is used. What am I doing wrong in the following code?

Function update_db()
    Dim daoengine As DAO.DBEngine
    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset
    dbs = daoengine.OpenDatabase("Project.mdb")
    rst = dbs.OpenRecordset("Log", dao.RecordsetTypeEnum.dbOpenDynaset)

End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2025-01-01 22:52:29

你应该有

Dim daoengine As New DAO.DBEngine

而不是

Dim daoengine As DAO.DBEngine

You should have

Dim daoengine As New DAO.DBEngine

instead of

Dim daoengine As DAO.DBEngine
明媚殇 2025-01-01 22:52:29

当你说

Dim daoengine As DAO.DBEngine

你只是创建一个变量 daoengine 时,但它实际上并不指向任何东西。之后您需要执行此操作:

Set daoengine = New DAO.DBEngine

您也可以输入 Dim daoengine As New DAO.DBEngine,但最好在上面的两行中执行此操作。如果您将 New 放在 Dim 行中,您就创建了所谓的“自动实例化”变量,该变量可以在您认为已处理掉之后神奇地再次生效它。

有关更多详细信息,请参阅此处的不要使用自动实例化对象变量声明变量(在 VBA 中)

When you say

Dim daoengine As DAO.DBEngine

you're just creating a variable, daoengine, but it doesn't actually point to anything. You need to do this afterwards:

Set daoengine = New DAO.DBEngine

You can also type Dim daoengine As New DAO.DBEngine, but it's better to do it in the two lines above. If you put the New in the Dim line, you create what's called an "auto-instancing" variable which can magically come to life again after you thought you'd disposed of it.

For more details see the Don't Use Auto-Instancing Object Variables here: Declaring Variables (in VBA)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文