Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 指定对象时出现VBA错误91的原因可能是什么_Excel_Vba_Ms Access_Ms Access 2016 - Fatal编程技术网

Excel 指定对象时出现VBA错误91的原因可能是什么

Excel 指定对象时出现VBA错误91的原因可能是什么,excel,vba,ms-access,ms-access-2016,Excel,Vba,Ms Access,Ms Access 2016,我在Excel中有一个宏,用于从Access数据库查询数据。这对我来说很好。我与几位同事共享了该文件,其中两位一直出现错误“91对象变量或未设置块变量” 调试表明这一行是罪魁祸首 Set rs = objAccess.CurrentProject.Connection.Execute(SQL) 感谢您能分享的任何见解。相关代码如下 Sub RefreshData() On Error GoTo SubError Const DbLoc As String = "path to

我在Excel中有一个宏,用于从Access数据库查询数据。这对我来说很好。我与几位同事共享了该文件,其中两位一直出现错误“91对象变量或未设置块变量”

调试表明这一行是罪魁祸首

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
子刷新数据()
关于错误转到子错误
Const DbLoc As String=“指向.accdb的路径”
作为对象的暗淡对象
将遥感器作为对象
将书本作为工作簿
将工作表设置为工作表
我认为时间很长
将SQL设置为字符串
常量cstrPwd为String=“foo”
'设置对工作簿和工作表的引用
设置xlBook=active工作簿
如果这本书什么都不是
MsgBox“未找到xlBook”
如果结束
设置xlSheet=xlBook.工作表(2)
如果xlSheet什么都不是,那么
MsgBox“未找到xlSheet”
如果结束
xlSheet.范围(“A5:BA99000”).ClearContents
'与用户通信
Application.StatusBar=“连接到外部数据库…”
Application.Cursor=xlWait
'连接到Access数据库
出错时继续下一步
Set objAccess=GetObject(,“Access.Application”)
如果错误号为0,则
Set objAccess=CreateObject(“Access.Application”)
如果结束
关于错误转到子错误
objAccess.Visible=False
objAccess.OpenCurrentDatabase DbLoc,cstrPwd
SQL=“选择*自[Access中预定义的选择查询的名称]”
'执行我们的查询并填充记录集
设置rs=objAccess.CurrentProject.Connection.Execute(SQL)'罪魁祸首:)
如果rs什么都不是
MsgBox“rs未找到。SQL=“&SQL
如果结束
'将记录集复制到电子表格
Application.StatusBar=“写入电子表格…”
如果rs.RecordCount=0,则
MsgBox“没有从数据库检索到数据”,vbInformation+vbOKOnly,“没有数据”
转到次出口
其他的
莫夫拉斯特
recCount=rs.RecordCount
先走一步
如果结束
xlSheet.Range(“A5”).CopyFromRecordset rs
Application.StatusBar=“更新完成”
子出口:
出错时继续下一步
Application.Cursor=xlDefault
rs.Close
设置rs=无
objAccess.退出
设置对象访问=无
Set xlSheet=无
设置xlBook=Nothing
出口接头
子错误:
Application.StatusBar=“”
MsgBox“RefreshData-UpdateData VBA错误:”&vbCrLf&Err.Number&“=”&Err.Description
恢复子出口
端接头

注意:我正在使用中建议的对象,因为这是处理加密的.accdb的唯一方法。

我会仔细检查连接

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

SQL=“从MSysObjects中选择Id”
'执行我们的查询并填充记录集
MsgBox objAccess.CurrentProject.Connection
设置rs=objAccess.CurrentProject.Connection.Execute(SQL)
MsgBox rs!身份证件

currentProject为null或currentProject。连接为null。您确定您的同事可以访问与您完全相同路径的文件吗?也就是说,它在本地机器上,或者如果在共享服务器上,它们的驱动器映射相同?@Marc是的,我确信。在发生故障的用户机器上,
objAccess.CurrentProject.Connection
中的一个对象未设置。如果您打破这些对象的设置,并测试每个步骤是否成功,您将找出问题所在。照目前的情况,这个问题是无法回答的,如果没有更新以提供更多信息,应该closed@Programnik你是对的!对于我的同事,两者都是空的,而对于我,两者都不是空的。因此,他们和我一起工作,而不是和他一起工作。知道怎么修吗?