简体   繁体   English

在 Excel 2016 中运行 VBA 时出现 OLE 错误?

[英]OLE error when running VBA in Excel 2016?

I'm trying to use Excel as a Database, and I'm following a tutorial from this site .我正在尝试将 Excel 用作数据库,并且正在学习 本网站的教程。

Problem is, whenever I try to "Update Drop Downs" in the file below, I get this error: "Microsoft is waiting for another application to complete an OEL action".问题是,每当我尝试在下面的文件中“更新下拉菜单”时,都会收到此错误:“Microsoft 正在等待另一个应用程序完成 OEL 操作”。

What am I missing or doing wrong here, and how do I get this right?我在这里遗漏了什么或做错了什么,我该如何做对?

I'm using Excel 2016 Home & Student that's uptodate.我正在使用最新的 Excel 2016 Home & Student。 I also enable Macros when opening the Workbook.我还在打开工作簿时启用宏。

The same file runs perfect when open in Excel 2007. I've also noticed that Microsoft ActiveX Data Objects 6.0 Library references a "msado60.dll" in the example, whereas, it's a "msado60.tlb" file in Excel 2016 (which I use).在 Excel 2007 中打开时,相同的文件运行良好。我还注意到 Microsoft ActiveX 数据对象 6.0 库在示例中引用了“msado60.dll”,而它是 Excel 2016 中的“msado60.tlb”文件(我用)。

Link to Excel File 链接到 Excel 文件

Private Sub cmdShowData_Click()
    'populate data
    strSQL = "SELECT * FROM [data$] WHERE "
    If cmbProducts.Text <> "" Then
        strSQL = strSQL & " [Product]='" & cmbProducts.Text & "'"
    End If

    If cmbRegion.Text <> "" Then
        If cmbProducts.Text <> "" Then
            strSQL = strSQL & " AND [Region]='" & cmbRegion.Text & "'"
        Else
            strSQL = strSQL & " [Region]='" & cmbRegion.Text & "'"
        End If
    End If

    If cmbCustomerType.Text <> "" Then
        If cmbProducts.Text <> "" Or cmbRegion.Text <> "" Then
            strSQL = strSQL & " AND [Customer Type]='" & cmbCustomerType.Text & "'"
        Else
            strSQL = strSQL & " [Customer Type]='" & cmbCustomerType.Text & "'"
        End If
    End If

    If cmbProducts.Text <> "" Or cmbRegion.Text <> "" Or cmbCustomerType.Text <> "" Then
        'now extract data
        closeRS

        OpenDB

        rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
        If rs.RecordCount > 0 Then
            Sheets("View").Visible = True
            Sheets("View").Select
            Range("dataSet").Select
            Range(Selection, Selection.End(xlDown)).ClearContents

            'Now putting the data on the sheet
            ActiveCell.CopyFromRecordset rs
        Else
            MsgBox "I was not able to find any matching records.", vbExclamation + vbOKOnly
            Exit Sub
        End If

        'Now getting the totals using Query
        If cmbProducts.Text <> "" And cmbRegion.Text <> "" And cmbCustomerType.Text <> "" Then
            strSQL = "SELECT Count([data$].[Call ID]) AS [CountOfCall ID], [data$].[Resolved] " & _
            " FROM [Data$] WHERE ((([Data$].[Product]) = '" & cmbProducts.Text & "' ) And " & _
            " (([Data$].[Region]) =  '" & cmbRegion.Text & "' ) And (([Data$].[Customer Type]) =  '" & cmbCustomerType.Text & "' )) " & _
            " GROUP BY [data$].[Resolved];"

            closeRS
            OpenDB

            rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
            If rs.RecordCount > 0 Then
                Range("L6").CopyFromRecordset rs
            Else
                Range("L6:M7").Clear
                MsgBox "There was some issue getting the totals.", vbExclamation + vbOKOnly
                Exit Sub
            End If
        End If
    End If
End Sub

Private Sub cmdUpdateDropDowns_Click()
    strSQL = "Select Distinct [Product] From [data$] Order by [Product]"
    closeRS
    OpenDB
    cmbProducts.Clear

    rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
    If rs.RecordCount > 0 Then
        Do While Not rs.EOF
            cmbProducts.AddItem rs.Fields(0)
            rs.MoveNext
        Loop
    Else
        MsgBox "I was not able to find any unique Products.", vbCritical + vbOKOnly
        Exit Sub
    End If

    '----------------------------
    strSQL = "Select Distinct [Region] From [data$] Order by [Region]"
    closeRS
    OpenDB
    cmbRegion.Clear

    rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
    If rs.RecordCount > 0 Then
        Do While Not rs.EOF
            cmbRegion.AddItem rs.Fields(0)
            rs.MoveNext
        Loop
    Else
        MsgBox "I was not able to find any unique Region(s).", vbCritical + vbOKOnly
        Exit Sub
    End If
    '----------------------
    strSQL = "Select Distinct [Customer Type] From [data$] Order by [Customer Type]"
    closeRS
    OpenDB
    cmbCustomerType.Clear

    rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
    If rs.RecordCount > 0 Then
        Do While Not rs.EOF
            cmbCustomerType.AddItem rs.Fields(0)
            rs.MoveNext
        Loop
    Else
        MsgBox "I was not able to find any unique Customer Type(s).", vbCritical + vbOKOnly
        Exit Sub
    End If
End Sub

在此处输入图片说明

Per the comments, your OpenDB method is opening an ADO connection.根据评论,您的OpenDB方法正在打开 ADO 连接。 You don't appear to be closing it anywhere.你似乎没有在任何地方关闭它。

You're attempting to reopen a connection that is already open.您正在尝试重新打开已打开的连接。 The OLE server error is telling you that the server (Excel) is busy because there is already another ADO connection attached to it. OLE 服务器错误告诉您服务器 (Excel) 正忙,因为已经有另一个 ADO 连接连接到它。 All you should need to do is make sure that you only open the connection once , and then close it when you are done working with it.您需要做的就是确保只打开一次连接,然后在使用完后关闭它。

I've had a similar issue.我有过类似的问题。 This worked for me:这对我有用:
1. On the Tools menu, click Options. 1. 在工具菜单上,单击选项。
2. Click the General tab. 2. 单击常规选项卡。
3. Change the Ignore other applications that use Dynamic Data Exchange (DDE) check box, and then click OK. 3. 更改忽略使用动态数据交换 (DDE) 的其他应用程序复选框,然后单击确定。

I would only recommend changing this setting while working with your tutorial.我只建议在使用您的教程时更改此设置。 While it solved this problem for me, it also caused Excel to behave strangely in some other circumstances.虽然它为我解决了这个问题,但它也导致 Excel 在其他一些情况下表现异常。

If you think the issue is tied to your specific version of ADO you could also try using a reference to an older version (such as Microsoft ActiveX Data Objects 2.8 Library).如果您认为问题与您的特定 ADO 版本有关,您还可以尝试使用对旧版本(例如 Microsoft ActiveX Data Objects 2.8 Library)的引用。

I just tested your code (Excel 2013 installed) and everything was fine though.我刚刚测试了您的代码(安装了 Excel 2013),但一切都很好。 No errors occured or something like that.没有发生错误或类似的事情。 I also checked the reference to the Microsoft ActiveX Data Objects Library and it is the ".tlb" for me too.我还检查了对 Microsoft ActiveX 数据对象库的引用,它对我来说也是“.tlb”。 So I think that this is not the problem.所以我认为这不是问题。

But there is a problem that I think could be the reason for your error:但是我认为有一个问题可能是导致您出错的原因:

When your code line rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic is called the macro code could probably continue running and calling the next line while the SQL-query is not yet done.当您的代码行rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic被调用时,宏代码可能会继续运行并在 SQL 查询尚未完成时调用下一行。 So calling the rs.RecordCount in the next line could result in an error if the query is still running.因此,如果查询仍在运行,则在下一行调用rs.RecordCount可能会导致错误。

As I was not able to reproduce your error, I can't to do further testing to solve your issue.由于我无法重现您的错误,因此无法进行进一步测试来解决您的问题。 So hopefully my idea might help you or anyone else to solve your problem.所以希望我的想法可以帮助您或其他任何人解决您的问题。

This might be an old issue but my recent experience with this error:这可能是一个老问题,但我最近遇到这个错误:

Testers with my macro got this error and the issue was testers did not have modify access to the folder where workbook was opened and saved.使用我的宏的测试人员收到此错误,问题是测试人员无法修改打开和保存工作簿的文件夹的访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM