Excel在ADODB连接打开命令上崩溃

问题描述 投票:2回答:2

我正在尝试在Excel(2013)中使用vba来使用ADODB连接和SQL来查询和整理来自另一个选项卡的数据。我的笔记本电脑正在运行Windows 10 Pro 64位(10.0.17763)和Excel Standard 2013 64位(15.0.5233.1000)

我曾尝试删除并重新添加Active X引用,但尚未解决问题。我还尝试过删除所有可能的引用,并一一重新添加它们,以及尝试使用旧版本。所有结果都相同。我选择的引用是:应用程序的Visual Basic,Microsoft Excel 15.0对象库,OLE自动化,Microsoft Office 15.0对象库,Microsoft Forms 2.0对象库,Microsoft ActiveX数据对象6.1库,Microsoft ActiveX数据对象Recordset 6.0库。

当我进入下面的代码中的cnExcel.Open时,excel只是崩溃(即关闭),没有错误消息。

Dim cnExcel As Connection
Dim rsExcel As Recordset
   .....
strFile = ThisWorkbook.FullName
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=""C:\Users\trheinsc\Desktop\InProgress\Error Dashboard_TEMPLATE.xlsm"";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Set cnExcel = CreateObject("ADODB.Connection")
Set rsExcel = CreateObject("ADODB.Recordset")
rsExcel.CursorLocation = adUseClient
cnExcel.Open strCon                     ' <=  This is where Excel blows up and shuts down.

' Populate Iteration Columns
sqlSelect = "SELECT DISTINCT [Iteration]"
sqlFrom = "FROM [ErrorDetail$]"
sqlWhere = "WHERE [Iteration] Is Not NULL"
sqlGroup = ""
sqlOrder = "ORDER BY [Iteration] DESC"
sqlString = sqlSelect & " " & sqlFrom & " " & sqlWhere & " " & sqlGroup & " " & sqlOrder
rsExcel.Open sqlString, cnExcel
cDashRow = 1
cDashCol = FirstIterCol
Do
    If rsExcel.EOF Then Exit Do
    shDashboard.Cells(cDashRow, cDashCol) = "Iter " & rsExcel(0) & Chr(10) & "# Errors"
    shDashboard.Cells(cDashRow, cDashCol).Columns.AutoFit
    aIterArray(cDashCol) = rsExcel(0)
    cDashCol = cDashCol + 1
    rsExcel.MoveNext
Loop Until rsExcel.EOF
LastIterCol = cDashCol - 1
If rsExcel.State = 1 Then rsExcel.Close

任何帮助将不胜感激。

sql excel vba activex adodb
2个回答
2
投票

很明显是连接字符串问题(不是总是这样吗?)。在Excel 2013中连接到XLSM文件时,扩展属性需要包含“ Excel 12.0 Marco”,而不是“ Excel 12.0”。


0
投票

我这样使用它。

Dim Rs As Object
Dim strConn As String
Dim i As Integer

strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=" & ThisWorkbook.FullName & ";" & _
        "Extended Properties=Excel 12.0;"


Set Rs = CreateObject("ADODB.Recordset")
Rs.Open strSQL, strConn
© www.soinside.com 2019 - 2024. All rights reserved.