首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >2016 -如何修复?

2016 -如何修复?
EN

Stack Overflow用户
提问于 2022-07-05 14:07:01
回答 1查看 396关注 0票数 0

我继承了一个Excel宏,该宏在Excel 2013中运行良好(文件以XLS格式保存,并似乎使用32位ODBC Microsoft Excel驱动程序),当我试图在Excel 2016中运行相同的宏时(该宏似乎使用64位Excel驱动程序),会出现以下错误。

未找到Microsoft数据源名称,也未指定默认驱动程序。

失败的代码如下所示,它特别突出了"With cn“部分中的".Open”。关于为新Excel重新配置它的想法?

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2022-07-06 06:54:47

在查看了@匿名建议之后,我使用外部连接来显示连接字符串。在执行我的宏时,这没有出错,并且似乎正确地处理了SQL。

代码语言:javascript
复制
With cn
    .ConnectionString = "DSN=Excel Files;" & _
    "DBQ=" & strFile & "DriverId=1046; ReadOnly=False;"
    .Open
End With
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72870838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档