我继承了一个Excel宏,该宏在Excel 2013中运行良好(文件以XLS格式保存,并似乎使用32位ODBC Microsoft Excel驱动程序),当我试图在Excel 2016中运行相同的宏时(该宏似乎使用64位Excel驱动程序),会出现以下错误。
未找到Microsoft数据源名称,也未指定默认驱动程序。
失败的代码如下所示,它特别突出了"With cn“部分中的".Open”。关于为新Excel重新配置它的想法?
Function ReportGroup(strSQL33 As String) As Recordset
Dim ocmd As Command
Set ocmd = New Command
Dim ors As ADODB.Recordset
Dim r As Long
Dim intIndx As Long
Dim cn As ADODB.Connection
Dim strFile As String
strFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";"
Set ors = Nothing
Set cn = Nothing
Set cn = New ADODB.Connection
Set ors = New Recordset
'The connection
With cn
.Provider = "MSDASQL"
.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};" & _
"DBQ=" & strFile & "; ReadOnly=False;"
.Open
End With
With ocmd
.CommandText = strSQL33
.CommandType = adCmdText
.ActiveConnection = cn
Set ors = .Execute
End With
Set ReportGroup = ors
End Function发布于 2022-07-06 06:54:47
在查看了@匿名建议之后,我使用外部连接来显示连接字符串。在执行我的宏时,这没有出错,并且似乎正确地处理了SQL。
With cn
.ConnectionString = "DSN=Excel Files;" & _
"DBQ=" & strFile & "DriverId=1046; ReadOnly=False;"
.Open
End Withhttps://stackoverflow.com/questions/72870838
复制相似问题