0
我想在訪問數據庫中做一個查詢,然後得到這個查詢結果並插入到一個Excel工作表,但沒有任何工作。這是我的代碼如何在Excel中獲取Access查詢的結果?
Sub Analise()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim sSQL As String
Sheets("Plan1").Range("A2:A23").Select
For Each Regra In Selection
If Regra = "MYRULE" Then
sSQL = "SELECT ITEM !, ITEM2 FROM base WHERE NomeRegra = ""'MyValue'"" "
MyConn = "MyPath\base.mdb"
Set cnn = New ADODB.Connection
With cnn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open MyConn
End With
Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open Source:=sSQL, ActiveConnection:=cnn, CursorType:=AdForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText
Sheets("Plan1").Range("D1:E1") = Array("Field1", "Field2")
Range("D2:E100000").CopyFromRecordset rst
rst.Close
cnn.Close
End If
Next Regra
看如何問一個[mcve] –
試試這個教程:https://support.microsoft.com/en-us/kb/246335 –