在Access 2007中,我正在搜索一个表以查找某个ID。用户IDTxt在文本框中提供ID地址。
我收到错误80040e10,一个或多个参数没有值。

Private Sub Search_Click()

 'here a new part I'm not sure about...
Dim cn As ADODB.Connection
Dim rsQ As ADODB.Recordset

Set cn = CurrentProject.Connection
Set rsQ = New ADODB.Recordset

Dim strSQL As String

Dim ID_number As Integer
Dim blnExists As Boolean
blnExists = False
ID_number = IDTxt.Value

strSQL = "SELECT * FROM Table1  WHERE ID = ID_number;"
rsQ.Open strSQL, cn, adOpenStatic

If rsQ.RecordCount <> 0 Then
     ' Found it
    blnExists = True
    MsgBox "found"
Else
    MsgBox "not found"
End If

End Sub

最佳答案

您传递一个字符串而不替换该值;

strSQL = "SELECT * FROM Table1  WHERE ID = ID_number;"


需要是

strSQL = "SELECT * FROM Table1  WHERE ID = " & ID_number


因为ID_number仅在字符串之外的VBA变量的上下文中。

(您还没有在ID_number上执行类型检查,因此不受限制的文本框中的文本可能会出错,并且对于字符串参数来说是注入漏洞)

另请注意,RecordCount可以根据光标位置/类型返回-1

09-10 17:59