我有一个程序,它将从数据库(MS Access 2007)中找到一个记录,其中含有一个错误:查询表达式中的语法错误'%1%'的名字出错'VB6查找问题使用SQL(MS Access 2007)
这是我的代码:
Dim find As String
find = txtfind.Text
If txtfind.Text <> "" Then
Set rs = db.OpenRecordset("SELECT * from records WHERE firstname like '*'" & find & "'*")
If rs.EOF = True Then
MsgBox "No Record Found!", vbCritical + vbOKOnly, "Error"
Else
Text1 = rs(0)
Text2 = rs(1)
Text3 = rs(2)
Text4 = rs(3)
End If
If Not rs Is Nothing Then
Set rs = Nothing
Else
rs.Close
End If
End If
是不是访问通配符'*'而不是'%'? – 2011-04-06 09:17:44
这样:''从记录WHERE firstname'*'“&find&”'*“'的SELECT *将产生'SELECT *从记录WHERE firstname like'*'1''。我相信它应该是''SELECT *从记录WHERE名字像'*“&find&”*'“'(注意我删除了一个** **并且改变了另一个的位置)。 – ssarabando 2011-04-06 09:31:48
你在'WHERE'子句中防范SQL注入吗?请参阅http://stackoverflow.com/questions/512174/non-web-sql-injection/515150#515150 – onedaywhen 2011-04-06 15:08:30