【问题标题】:Working code in Excel 2003 throws runtime error in Windows 7Excel 2003 中的工作代码在 Windows 7 中引发运行时错误
【发布时间】:2012-07-20 16:57:56
【问题描述】:

我在 Windows XP 32 位机器上的 Microsoft Excel 2003 中创建了以下宏,当我按下刷新按钮时,我的电子表格会按应有的方式填充。

但是,当我在 Windows 7 机器(32 位和 64 位)的用户机器上运行此程序时,我收到以下错误消息

“运行时错误'-2147467259 (80004005)': [Microsoft][ODBC Driver Manager] 未找到数据源名称,未指定默认驱动程序”

Private Sub CommandButton1_Click()
    Dim cmd As New ADODB.Command
    Dim conn As ADODB.Connection
    Dim prm As ADODB.Parameter
    Dim strConn As String
    Dim strSQL As String
    Dim Rst As ADODB.Recordset
    Dim WSP As Worksheet
    Dim lastRow As Long
    Dim ranges As range



 strConn = "Data Source=;Initial Catalog=;User Id=;Trusted_Connection=False;"
    Set conn = New ADODB.Connection
    Set WSP = Worksheets("KPI")

    lastRow = WSP.Cells.SpecialCells(xlCellTypeLastCell).Row
    Set ranges = WSP.range("A6", WSP.Cells(lastRow, "K"))
    ranges.Clear

    conn.Open strConn

    Set cmd = New ADODB.Command
    cmd.CommandText = "dbo.returns_kpi_data"
    cmd.CommandType = adCmdStoredProc
    cmd.ActiveConnection = conn

    cmd.Parameters.Refresh
    cmd.Parameters("@OrderDate1").Value = WSP.range("G3", "G3")
    cmd.Parameters("@OrderDate2").Value = WSP.range("I3", "I3")


    'Execute the Stored Procedure
    Set Rst = cmd.Execute

    range("A6").CopyFromRecordset Rst


    'Close the connection
    conn.Close
End Sub

【问题讨论】:

    标签: excel vba windows-7 odbc adodb


    【解决方案1】:

    我猜您正在连接到 SQL Server。在对连接字符串进行模糊处理时,您已删除任何可用于正确识别问题的信息。我怀疑您可能在其他 PC 上使用 DSN,因为您甚至在该字符串中都没有提供程序。您可以在此处获取有关连接字符串的信息:http://www.connectionstrings.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 2013-03-01
      • 1970-01-01
      • 2022-10-09
      相关资源
      最近更新 更多