首页 > 解决方案 > DAO.DBEngine 类不再使用 Windows 10 在 MS Access 2016 中注册

问题描述

客户端最近从 Windows 7 升级到 10 并从 Access 2013 移动到 2016(包含在 Office 365 中)。

Excel 中的 VBA 宏现在会生成以下错误:

“运行时错误 '-2147221164 (80040154)' 类未注册。” 在线:

 Set myEngine = New DAO.DBEngine

我确认 DAO 3.6 包含在参考资料中。一个站点建议“修复”Office 安装,但我没有这样做。

这个回应建议转向 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)

好像我在这里遗漏了一些基本的东西。任何帮助表示赞赏。

标签: excelvbaadodao

解决方案


我建议使用后期绑定来实现代码可移植性。一旦你得到这个工作,你会发现它稍后在另一台计算机上失败。将所有内容声明为对象,然后根据需要使用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 开始,而不会违反时空连续体。


推荐阅读