2011-08-17 64 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