查询表达式中的语法错误(缺少运算符)

时间:2015-05-05 10:10:24

标签: ms-access

我正在尝试在MS Access 2013中创建搜索表单,每次我在创建的任何字段中插入搜索条件时都会收到错误消息

  查询表达式中的

“语法错误(缺少运算符)”* FROMWHERE(FirstName),如“Godswill”“AND”

在下面找到代码

<blink>
Private Sub cmdSearch_Click()
On Error GoTo errr
Me.qryCandInfo_subform.Form.RecordSource = "SELECT*FROM" & BuildFilter
Me.qryCandInfo_subform.Requery
Exit Sub
errr:
MsgBox Err.Description
End Sub
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim tmp As String
tmp = """"
Const conSetDate = "\#dd\/mm\/yyyy\#"
varWhere = Null

If Me.txtEnrNo > "" Then
varWhere = varWhere & "(Enr_No) like " & Me.txtEnrNo & "AND"
End If

If Me.txtFirstName > "" Then
varWhere = varWhere & "(First_Name) like " & tmp & Me.txtFirstName & tmp & "AND"
End If

If Me.txtDateFrom > "" Then
varWhere = varWhere & "((Admisssion_Year)>= " & Format(Me.txtDateFrom, conSetDate) & ") AND"
End If

If Me.txtDateTo > "" Then
varWhere = varWhere & "((Admisssion_Year)<= " & Format(Me.txtDateTo, conSetDate) & ") AND"
End If

If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE" & varWhere

If Right(varWhere, 5) = "AND" Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
End Function

1 个答案:

答案 0 :(得分:0)

您的代码存在很多问题 为了方便您和所涉及的每个人:在每个关键字之前和之后添加空格:

"SELECT*FROM" & BuildFilter => "SELECT * FROM " & BuildFilter
& "AND" => & " AND "
varWhere = "WHERE" & varWhere => varWhere = " WHERE " & varWhere
& ") AND" => & ") AND "

此外,您缺少指定要从中选择的表格...
此外,您的SQL有一个尾随AND。要么照顾它,要么选择不那么完美的追加1varWhere = " WHERE " & varWhere & " 1"的方法。 (您的版本检查以5个字符结尾并与And进行比较 - 这绝不应该是真的......)

此外:SQL注入......我怀疑pre {和""""改变了什么