【问题标题】:What could be the reason for VBA error 91 when assigning object分配对象时出现 VBA 错误 91 的原因可能是什么
【发布时间】:2021-02-28 08:22:21
【问题描述】:

我在 Excel 中有一个宏,可以从 Access 数据库中查询数据。它对我很好。我与几个同事共享了文件,其中两个人不断收到错误“91 Object variable or With block variable not set”。

调试表明这条线是罪魁祸首。

Set rs = objAccess.CurrentProject.Connection.Execute(SQL)

感谢您可以分享的任何见解。相关代码如下。

Sub RefreshData()
On Error GoTo SubError
    Const DbLoc As String = "path to .accdb"
    Dim objAccess As Object 
    Dim rs As Object 
    Dim xlBook As Workbook
    Dim xlSheet As Worksheet
    Dim recCount As Long
    Dim SQL As String
    Const cstrPwd As String = "foo"
 
    'Setup references to workbook and sheet
    Set xlBook = ActiveWorkbook
    
    If xlBook Is Nothing Then
        MsgBox "xlBook not found"
    End If
    
    Set xlSheet = xlBook.Worksheets(2)
    
    If xlSheet Is Nothing Then
        MsgBox "xlSheet not found"
    End If
    
    xlSheet.Range("A5:BA99000").ClearContents
   
    'Communicate with the user
    Application.StatusBar = "Connecting to an external database..."
    Application.Cursor = xlWait
 
    ' connect to the Access database
    On Error Resume Next
    Set objAccess = GetObject(, "Access.Application")
    If Err.Number <> 0 Then
        Set objAccess = CreateObject("Access.Application")
    End If
    On Error GoTo SubError
    objAccess.Visible = False
    objAccess.OpenCurrentDatabase DbLoc, , cstrPwd

    SQL = "SELECT * FROM [name of predefined select query in Access]"
    
    'Execute our query and populate the recordset
    Set rs = objAccess.CurrentProject.Connection.Execute(SQL) ' The culprit :)
 
    If rs Is Nothing Then
        MsgBox "rs not found. SQL=" & SQL
    End If
 
    'Copy recordset to spreadsheet
    Application.StatusBar = "Writing to spreadsheet..."
    If rs.RecordCount = 0 Then
        MsgBox "No data retrieved from database", vbInformation + vbOKOnly, "No Data"
        GoTo SubExit
    Else
        rs.MoveLast
        recCount = rs.RecordCount
        rs.MoveFirst
    End If
   
    xlSheet.Range("A5").CopyFromRecordset rs
    Application.StatusBar = "Update complete"

 
SubExit:
On Error Resume Next
    Application.Cursor = xlDefault
    rs.Close
    Set rs = Nothing
    objAccess.Quit
    Set objAccess = Nothing
    Set xlSheet = Nothing
    Set xlBook = Nothing
    Exit Sub
 
SubError:
    Application.StatusBar = ""
    MsgBox "RefreshData - UpdateData VBA error: " & vbCrLf & Err.Number & " = " & Err.Description
    Resume SubExit
   
End Sub

注意:我正在使用 this answer 中建议的对象,因为这是与我的加密 .accdb 一起工作的唯一方法。

【问题讨论】:

  • currentProject 为 null 或 currentProject.Connection 为 null。
  • 您确定您的同事可以访问与您完全相同路径的文件吗?即,它在他们的本地机器上,或者如果在共享服务器上,他们的驱动器映射相同?
  • @Marc 是的,我确定。
  • 在失败的用户机器上,objAccess.CurrentProject.Connection 中的对象之一未设置。如果您分解这些对象的设置并测试每个步骤是否成功,您将找出问题所在。照原样,这个 Q 是无法回答的,如果没有更新以提供更多信息,应该关闭
  • @Programnik 你是对的!对我的同事来说两者都是空的,而对我来说两者都不是空的。因此,他们和我一起工作,而不是他。知道如何解决吗?

标签: excel vba ms-access ms-access-2016


【解决方案1】:

我会仔细检查连接。

此外,对于测试,请打开一个简单的测试查询以排除您的查询存在的问题:

    SQL = "SELECT Id FROM MSysObjects"
    ' Execute our query and populate the recordset
    MsgBox objAccess.CurrentProject.Connection
    Set rs = objAccess.CurrentProject.Connection.Execute(SQL)
    MsgBox rs!Id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2019-10-23
    • 1970-01-01
    • 2016-05-10
    • 2023-03-29
    • 2013-06-25
    • 2011-10-22
    相关资源
    最近更新 更多