错误2147217904从Excel中查询Access时,没有给出一个或多个必需参数的值

我在Excel中查询Access中的Table1 。 我正在过滤列的SampleType根据在用户SampleType上选中的三个标准的where子句。

我还需要根据where子句中的select从SampleID列中返回不同logging的数量。

我的代码可能有一些小问题,我无法弄清楚。 请帮忙。 谢谢

 Dim cnn As ADODB.Connection 'dim the ADO collection class Dim rs As ADODB.Recordset 'dim the ADO recordset class Dim dbPath As String Dim SQLwhere As String Dim StrSql As String Dim i As Integer Dim tar6 As String Dim tar7 As String Dim tar8 As String tar6 = "col" tar7 = "lun" tar8 = "mel" 'add error handling On Error GoTo errHandler: 'Disable screen flickering. Application.ScreenUpdating = False dbPath = "C:\Users\Temp1\Documents\Annie\MDL_IonTorrent.accdb" 'set the search variable Set cnn = New ADODB.Connection ' Initialise the collection class variable cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath 'Create the SQL statement to retrieve the data from table. SQLwhere = "WHERE " If CheckBox2 = True Then SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & tar6 & "%" & "' AND " 'count a total district records in column SampleID. End If If CheckBox3 = True Then SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & tar7 & "%" & "' AND " End If If CheckBox4 = True Then SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & tar8 & "%" & "' AND " End If StrSql = "SELECT COUNT(*) AS UniqueRecordsCount FROM (SELECT DISTINCT [SampleID] FROM [Table1]) " 'Remove the last AND applicable If SQLwhere = "WHERE " Then SQLwhere = "" Else SQLwhere = Left(SQLwhere, Len(SQLwhere) - 5) End If StrSql = StrSql & SQLwhere 'Create the ADODB recordset object. Set rs = New ADODB.Recordset 'assign memory to the recordset 'ConnectionString Open '--5 aguments-- 'Source, ActiveConnection, CursorType, LockType, Options rs.Open StrSql, cnn GetDistinctValue = rs("UniqueRecordsCount") MsgBox (GetDistinctValue) UserForm1.Controls("txtCrt6").Value = rs!UniqueRecordsCount 'Check if the recordset is empty. If rs.EOF And rs.BOF Then 'Close the recordet and the connection. rs.Close cnn.Close 'clear memory Set rs = Nothing Set cnn = Nothing 'Enable the screen. Application.ScreenUpdating = True 'In case of an empty recordset display an error. MsgBox "There are no records in the recordset!", vbCritical, "No Records" Exit Sub End If 'Close the recordset and the connection. rs.Close cnn.Close 'clear memory Set rs = Nothing Set cnn = Nothing 'Enable the screen. Application.ScreenUpdating = True 'error handler On Error GoTo 0 Exit Sub errHandler: 'clear memory Set rs = Nothing Set cnn = Nothing MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure FindSampleId" 

括号是在错误的地方。 例如,如果只是CheckBox2是真的,那么你的SQL语句就会像:

 "SELECT COUNT(*) AS UniqueRecordsCount FROM (SELECT DISTINCT [SampleID] FROM [Table1]) WHERE [Table1].[SampleType] LIKE '" & tar6 & "%" & "'" 

FROM [Table1])之后的圆括号FROM [Table1])需要移动到SQL的最后,因为否则,您将在只有一列的表格中检查[SampleType]的值 – [SampleID]

所以:

 StrSql = "SELECT COUNT(*) AS UniqueRecordsCount FROM (SELECT DISTINCT [SampleID] FROM [Table1] " 

那么稍后:

 If SQLwhere = "WHERE " Then SQLwhere = ")" Else SQLwhere = Left(SQLwhere, Len(SQLwhere) - 5) & ")" End If 

我遇到了同样的错误,经过几个小时的发现,我从Word文档中复制了一条SQL语句,Word用特殊格式的倾斜引号replace了标准引号(')。 我只是发现了将SQL语句复制到记事本并重试它之后的区别。