2011-08-17 104 views
4

我想從Excel中使用VBA查詢訪問文件,它在行rs.Open queryStatement, conn, adOpenStatic, adLockOptimistic上發出錯誤。錯誤是「運行時錯誤'-2147217904(80040e10)':沒有給出一個或多個必需參數的值。」Excel VBA查詢訪問失敗

Function queryAccess() 
'inputs: filterID, desired output 

Dim toSheet As Worksheet 
Set toSheet = ThisWorkbook.Sheets("Sheet3") 
Dim filterID As String 
filterID = "CH0002" 

Dim conn As ADODB.Connection 
Dim rs As ADODB.recordSet 
Dim connStr As String 
Dim queryStatement As String 
Dim cmd As ADODB.Command 

connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\rtpwfil03\etu2\cat\projects\asbestos\STREAMS TO101\Testing\TO101 Testing Data.mdb;Persist Security Info=False;" 
'conn.Provider=""Microsoft.Jet.OLEDB.4.0" 


Set conn = New ADODB.Connection 
Set rs = New ADODB.recordSet 
Set cmd = New ADODB.Command 

queryStatement = "SELECT Filters.NominalLoading FROM Filters WHERE Filters.FilterID=" & filterID & ";" 

conn.Open connStr 
Debug.Print connStr 
Debug.Print queryStatement 
rs.Open queryStatement, conn, adOpenStatic, adLockOptimistic 



toSheet.Range(toSheet.Cells(1, 1)).CopyFromRecordset rs 

conn.Close 
rs.Close 


End Function 

當我做了debug.print我connStr和queryStatement我分別得到這個,: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\rtpwfil03\etu2\cat\projects\asbestos\STREAMS TO101\Testing\TO101 Testing Data.mdb;Persist Security Info=False;

SELECT Filters.NominalLoading FROM Filters WHERE Filters.FilterID=CH0002;

我得到了連接字符串從http://www.connectionstrings.com/access-2007 「標準安全」

關於如何解決此錯誤的任何想法?

回答

4

您需要在您的查詢中CH002字符串的單引號。變化:

queryStatement = "SELECT Filters.NominalLoading FROM Filters WHERE Filters.FilterID=" & filterID & ";" 

queryStatement = "SELECT Filters.NominalLoading FROM Filters WHERE Filters.FilterID='" & filterID & "';" 
+0

是的!我知道它必須是簡單的! – Brad