MS Access - 按文本框搜索

时间:2017-08-25 18:06:50

标签: ms-access

我对Microsoft 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'语句错误(缺少运算符)。有人可以帮我确定一下这里的语法错误是什么?非常感谢你提供的任何帮助。

乔恩

2 个答案:

答案 0 :(得分:0)

使用运算符(例如:=,<,>,LIKE),您可以比较两个值。

LIKE

之后,你错过了HousingLocation
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 & "*""))"

答案 1 :(得分: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