我在 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