我正在尝试在具有以下查询的数据库上连接excel。
SELECT * FROM Products WHERE Order = [Enter Order]
但excel似乎无法找到此查询。它仅显示实际表和不使用参数的其他查询。
有没有办法在使用参数的查询上连接excel?我正在使用MS-Excel 2007。
答案 0 :(得分:2)
一些注释。
“必须在Microsoft Query中创建参数查询。”
Customize a parameter query
Use Microsoft Query to retrieve external data
ADODB& VBA
''Ref: Microsoft ActiveX Data Objects x.x Library
Dim cmd As New ADODB.Command
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim prm As ADODB.Parameter
Dim strConn As String
strConn = "Provider = Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=z:\docs\test.accdb"
conn.Open strConn
cmd.ActiveConnection = conn
cmd.CommandText = "Query4"
cmd.CommandType = adCmdStoredProc
Set prm = cmd.CreateParameter("EnterText", adVarWChar, adParamInput, 50)
cmd.Parameters.Append prm
cmd.Parameters("EnterText").Value = ActiveWorkbook.Sheets("Sheet5").[A2]
'Execute the Stored Procedure
Set rs = cmd.Execute
ActiveWorkbook.Sheets("Sheet8").Cells(2, 1).CopyFromRecordset rs
'Close the connection
conn.Close