问题描述
客户端最近从Windows 7升级到10,并从Access 2013迁移到2016(包括在Office 365中)。
Excel中的VBA宏现在会产生以下错误:
“运行时错误'-2147221164(80040154)'类未注册。”在此行:
Set myEngine = New DAO.DBEngine
我验证了DAO 3.6已包含在参考资料中。一个站点建议“修复” Office安装,但我没有做。
此response建议改用ADO,如果我能找到一些这样做的例子,我可能会愿意。
以下是相关代码:
Option Base 1
Sub importPLCDataFromAccess(monthToImport As Date)
'This sub imports Influent and Effluent Data from the Access Database PLC_Data.mdb
' This database reads records from the PLC board on a daily basis and was created
' using Automation Direct's PointOfView software for interfacing with PLC Boards
Dim myDbLocation As String
myDbLocation = "K:\Users\WWTP Computer\Documents\POV_Projects\PLC Interface\PLC_Data.mdb"
Dim myWorkbook As Workbook
'Skip spurious stuff ...
Dim myEngine As DAO.DBEngine
Dim myDB As DAO.Database
Dim myRecordSet As DAO.Recordset
Dim myWorkSpace As DAO.Workspace
'Skip more spurious stuff ...
Set myEngine = New DAO.DBEngine ' This is the offending line
Set myDB = myEngine.OpenDatabase(myDbLocation)
似乎我在这里缺少基本知识。任何帮助表示赞赏。
解决方法
我建议使用后期绑定来实现代码的可移植性。一旦开始运行,您将发现它稍后在另一台计算机上失败。将所有内容声明为对象,然后根据需要使用CreateObject
命令提取引用。
示例:
Public Function GetDBEngine() As Object
On Error Resume Next
'try 120
Set GetDBEngine = CreateObject("DAO.DBEngine.120")
If Err.Number <> 0 Then
'try 36
Err.Clear
Set GetDBEngine = CreateObject("DAO.DBEngine.36")
If Err.Number <> 0 Then
Set GetDBEngine = CreateObject("DAO.DBEngine.35")
Err.Clear
End If
End If
On Error Goto 0
End Function
Sub importPLCDataFromAccess(monthToImport As Date)
'This sub imports Influent and Effluent Data from the Access Database PLC_Data.mdb
' This database reads records from the PLC board on a daily basis and was created
' using Automation Direct's PointOfView software for interfacing with PLC Boards
Dim myDbLocation As String
myDbLocation = "K:\Users\WWTP Computer\Documents\POV_Projects\PLC Interface\PLC_Data.mdb"
Dim myWorkbook As Workbook
'Skip spurious stuff ...
Dim myEngine As Object
Dim myDB As Object
Dim myRecordSet As Object
Dim myWorkSpace As Object
'Skip more spurious stuff ...
Set myEngine = GetDBEngine
Set myDB = myEngine.OpenDatabase(myDbLocation)
脚注:
我们在这里的时候,我可以说出Option Base 1
吗?当然,还有其他一些方法可以使您的代码从1开始,而不违反时空连续体。