【发布时间】:2010-09-18 10:15:06
【问题描述】:
我正在尝试使查询工作,该查询从表单控件中获取值(有时只是字符串的第一部分)。我遇到的问题是它只在输入完整字符串时才返回记录。
即在姓氏框中,我应该可以输入 gr,它会弹出
绿色 灰色的 格雷厄姆
但目前除非使用完整的搜索字符串,否则它不会显示任何内容。
有问题的表单上有4个搜索控件,只有在填写框的情况下才会在查询中使用。
查询是:
SELECT TabCustomers.*,
TabCustomers.CustomerForname AS NameSearch,
TabCustomers.CustomerSurname AS SurnameSearch,
TabCustomers.CustomerDOB AS DOBSearch,
TabCustomers.CustomerID AS MemberSearch
FROM TabCustomers
WHERE IIf([Forms]![FrmSearchCustomer]![SearchMember] Is Null
,True
,[Forms]![FrmSearchCustomer]![SearchMember]=[customerid])=True
AND IIf([Forms]![FrmSearchCustomer].[SearchFore] Is Null
,True
,[Forms]![FrmSearchCustomer]![SearchFore] Like [customerforname] & "*")=True
AND IIf([Forms]![FrmSearchCustomer]![SearchLast] Is Null
,True
,[Forms]![FrmSearchCustomer]![SearchLast] Like [customersurname] & "*")=True
AND IIf([Forms]![FrmSearchCustomer]![Searchdate] Is Null
,True
,[Forms]![FrmSearchCustomer]![Searchdate] Like [customerDOB] & "*")=True;
【问题讨论】:
-
我不建议将 LIKE 运算符与看起来像日期字段的内容一起使用。您可能会得到各种意想不到的结果,例如用户输入 1 表示 1 月(或 1 日)并匹配 10 月、11 月、12 月(或 10 日至 19 日)。
-
您应该使用 application.buildCriteria 即时构建您的 WHERE 子句。这就是它所做的(我认为......)
-
BuildCriteria 是一个非常出色的工具。它是表达式服务的同一部分的编程接口,可将您在 QBE 的标准行中键入的内容解析为正确的语法。这是我最常用的非显而易见的访问命令之一。