2015-05-05 133 views
-1

我試圖創建在MS Access 2013搜索表單,每次我插入搜索條件中的任何字段創建我得到錯誤信息語法錯誤(缺少操作員)

「語法錯誤(操作符丟失)在查詢表達式 「* FROMWHERE(名字),如 」Godswill「」 和」

找到下面的代碼

<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 

+0

外貌就像你需要在查詢字符串中添加一些空白字符一樣''SELECT * FROM''應該可以是'「SELECT * FROM等等''' – phuzi

回答

0

你的代碼有很多錯誤。
爲了讓您輕鬆和參與大家:前添加空格,每個關鍵字後:

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

另外你缺少specfify要從選擇表...
而且你的SQL有尾隨AND。要麼照顧這個,要麼選擇並非如此完美的方式來添加1varWhere = " WHERE " & varWhere & " 1"。 (你的版本檢查與一些5個字符結尾且以And比較 - 應該不會爲真...)

此外:SQL注入......我懷疑,前和appening """"改變任何東西

相關問題