我正在尝试在Excel中运行Access数据库查询,但面临问题。
Dim dbs As DAO.Database
Set dbs = CurrentDb
dbs.Execute "DELETE FROM tblMyTable WHERE Bad", dbFailOnError
这里得到了
运行时错误424对象需要
完全在第二行
set dbs = CurrentDb
我已经添加了参考DAO 3.6对象库。应该保留什么代替CurrentDB
。我的MsAccess数据库位于本地磁盘中。
答案 0 :(得分:1)
CurrentDb
未被Excel识别,因此您将其视为空变体,因为您尚未为其分配任何内容。使用Option Explicit
将来可以防止出现此类问题。
如果您的数据库已经打开,请尝试以下操作:Set dbs = GetObject(,"Access.Application").CurrentDb
Option Explicit '### This goes at the VERY TOP of ALL MODULES
Sub foo()
Dim dbs As DAO.Database
Dim appAccess as Object '# Access.Application
Set appAccess = GetObject(,"Access.Application") '# Get a handle on Access
Set dbs = appAccess.CurrentDb
dbs.Execute "DELETE FROM tblMyTable WHERE Bad", dbFailOnError
End Sub