错误2147217904从Excel查询Access时,没有为一个或多个必需参数指定值

时间:2015-06-28 22:29:41

标签: excel excel-vba ms-access access-vba vba

我在Excel中查询 Table1 。我在where子句中根据 userform 上检查的三个条件过滤了列SampleType

我还需要根据where子句中的选择返回列SampleID中不同记录的数量。

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

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"

2 个答案:

答案 0 :(得分:1)

括号位于错误的位置。例如,如果只是CheckBox2为真,那么您的SQL语句最终会像:

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

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

答案 1 :(得分:0)

我遇到了同样的错误,经过几个小时后发现我从Word文档中复制了一条SQL语句,并且Word已经用特殊格式的倾斜引号替换了标准引号(')。在将SQL语句复制到记事本并重试之后,我才发现了差异。