2017-08-25 57 views
0

我對Microsoft Access相對來說比較新,但我從下面的另一個數據庫使用相同的代碼並將其替換爲您在下面看到的代碼。MS Access - 按文本框搜索

strsearch = Me.txtSearch.Value 
Task = "SELECT * FROM t_Cases WHERE ((CaseID Like ""*" & strsearch & "*"") OR (ActiveYN Like ""*" & strsearch & "*"") OR (ClaimType Like ""*" & strsearch & "*"") OR (HousingLocation ""*" & strsearch & "*"") OR (GrievanceAddress Like ""*" & strsearch & "*""))" 
Me.RecordSource = Task 
Me.OrderByOn = True 

有一個名爲txtsearch一個文本框和窗體上的記錄是指在書面文本框中選擇進行過濾。但是,我得到一個runtime error '3075'在查詢表達式中聲明語法錯誤(缺少運算符)。有人可以幫助我確定這是什麼語法錯誤?我非常感謝您提供的任何幫助。

喬恩

+0

您的表達式有兩個賦值運算符,它指向第一個賦值兩側的strSearch。除非您提供更多信息,否則無法修復。 – nicomp

+0

需要提供哪些附加信息才能確定要刪除哪個分配操作員? – swtched

+0

您需要提供很多,因爲您發佈的內容不可能用於/從其他數據庫中獲取。 – nicomp

回答

0

與運營商(例如:=,<,>,LIKE),你可以比較兩個值。

你缺少一個LIKEHousingLocation

Task = "SELECT * FROM t_Cases WHERE ((CaseID Like ""*" & strsearch & "*"")" & _ 
    " OR (ActiveYN Like ""*" & strsearch & "*"")" & _ 
    " OR (ClaimType Like ""*" & strsearch & "*"")" & _ 
    " OR (HousingLocation LIKE ""*" & strsearch & "*"")" & _ 
    " OR (GrievanceAddress Like ""*" & strsearch & "*""))" 
0

我們從來沒有使用簡單連接,用戶的輸入。如果用戶輸入包含",那麼我們的查詢中有一個語法錯誤。

讓我們使用BuildCriteria函數。

Dim strsearch As String 
    strsearch = Me.txtSearch.Value 

Dim filters(0 To 4) As String 
    filters(0) = BuildCriteria("CaseID", dbText, strsearch) 
    filters(1) = BuildCriteria("ActiveYN", dbText, strsearch) 
    filters(2) = BuildCriteria("ClaimType", dbText, strsearch) 
    filters(3) = BuildCriteria("HousingLocation", dbText, strsearch) 
    filters(4) = BuildCriteria("GrievanceAddress", dbText, strsearch) 

Dim filter As String 
    filter = Join(filters, " OR ") 

Dim Task As String 
    Task = "SELECT * FROM t_Cases WHERE " & filter 

Me.RecordSource = Task 
Me.OrderByOn = True