What could be the reason for VBA error 91 when assigning object

守給你的承諾、 提交于 2020-12-15 03:46:46

问题


I have a Macro in Excel that queries data from an Access database. It is working fine with me. I shared the file with a few colleagues and two of them keep getting the error "91 Object variable or With block variable not set".

Debugging indicates that this line is the culprit.

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

Appreciate any insights you can share. Relevant code below.

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

Note: I am using object as advised in this answer because it is the only way that worked with my encrypted .accdb.


回答1:


I would double-check the connection.

Also, for a test, open a simple test query to rule out issues with your query:

    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


来源:https://stackoverflow.com/questions/64869830/what-could-be-the-reason-for-vba-error-91-when-assigning-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!