2

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

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

“运行时错误 '-2147467259 (80004005)':[Microsoft][ODBC Driver Manager] Data source name not found an no default driver specified”

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
4

1 回答 1

3

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

于 2012-07-19T12:13:23.373 回答