|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
请教:用Excel VBA在Access里做参数查询的问题
各位,我用一下代码在VBA里做Acess参数查询时,没有任何结果,但在Access里查询时没有问题,是否在VBA里查询参数不能用*或?等通配符? 如需用该如何写代码?谢谢!
Sub RunAccessParamQuery(strQryName As String)
Dim cat As ADOX.Catalog
Dim cmd As ADODB.Command
Dim rst As ADODB.Recordset
Dim Wk1 As String
Dim Wk2 As String
Dim Wk3 As String
Dim Wk4 As String
Dim Wk5 As String
Wk1 = "2009*"
Wk2 = "2008*"
Wk3 = "2007*"
Worksheets("MacroPage").Activate
FFdr = Range("D5").Value & "\"
Bk1 = Range("B5").Value
Set cat = New ADOX.Catalog
cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & FFdr & Bk1
Set cmd = cat.Procedures(strQryName).Command
cmd.Parameters("[Week#1]") = "2009" & "*"
cmd.Parameters("[Week#2]") = Wk2
cmd.Parameters("[Week#3]") = Wk3
cmd.Parameters("[Week#4]") = ""
cmd.Parameters("[Week#5]") = ""
Set rst = cmd.Execute
Sheets("OutPut2").Select
For i = 0 To rst.Fields.Count - 1
Cells(1, i + 1).Value = rst.Fields(i).Name
Next
With ActiveSheet
.Range("A2").CopyFromRecordset rst
.Range(Cells(1, 1), Cells(1, rst.Fields.Count)).Font.Bold = True
.Range("A1").Select
End With
Selection.CurrentRegion.Columns.AutoFit
rst.Close
Set cmd = Nothing
Set cat = Nothing
End Sub |
|