4

我正在尝试使查询工作,该查询从表单控件中获取值(有时只是字符串的第一部分)。我遇到的问题是它仅在输入完整字符串时才返回记录。

即在姓氏框中,我应该可以输入 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;
4

5 回答 5

5

你有你的 LIKE 表达倒退。我重写了查询以删除不必要的 IIF 命令并修复 LIKE 运算符的操作数顺序:

SELECT TabCustomers.*
FROM TabCustomers
WHERE (Forms!FrmSearchCustomer!SearchMember Is Null Or Forms!FrmSearchCustomer!SearchMember=[customerid]) 
And (Forms!FrmSearchCustomer.SearchFore Is Null Or [customerforname] Like Forms!FrmSearchCustomer!SearchFore & "*") 
And (Forms!FrmSearchCustomer!SearchLast Is Null Or [customersurname] Like Forms!FrmSearchCustomer!SearchLast & "*") 
And (Forms!FrmSearchCustomer!Searchdate Is Null Or [customerDOB] Like Forms!FrmSearchCustomer!Searchdate & "*");

我通过复制最可能的情况来构建该查询:我创建了一个包含提到的字段的虚拟表,以及一个包含字段的表单和一个包含上面列出的查询的子表单,当按下搜索按钮时,它会刷新。如果您愿意,我可以提供我创建的示例的下载链接。该示例按预期工作。J 只拾取 Jim 和 John,而 John 或 Jo 只提取 John 记录。

于 2008-10-21T22:33:16.657 回答
5

有一种访问方法!

如果您在表单上有“过滤器”控件,为什么不使用 Application.buildCriteria 方法,这将允许您将过滤条件添加到字符串,然后使用该字符串进行过滤,并构建您的 WHERE即时条款?

selectClause = "SELECT TabCustomers.* FROM TabCustomers"
if not isnull(Forms!FrmSearchCustomer!SearchMember) then
    whereClause = whereClause & application.buildCriteria(your field name, your field type, your control value) &  " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchFore) then
    whereClause = whereClause & application.buildCriteria(...) &  " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchLast) then
    whereClause = whereClause & application.buildCriteria(...) &  " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchDate) then
    whereClause = whereClause & application.buildCriteria(...) & " AND "
endif
--get rid of the last "AND"
if len(whereClause) > 0 then
     whereClause = left(whereClause,len(whereClause)-5)
     selectClause = selectClause & " WHERE " & whereClause
endif
-- your SELECT instruction is ready ...

编辑: buildCriteria 将返回(例如):

  • 'field1 = "GR"'当您在控件中键入“GR”时
  • 'field1 LIKE "GR*"'当您输入"GR*"控件时
  • 'field1 LIKE "GR*" or field1 like "BR*"'如果您输入'LIKE "GR*" OR LIKE "BR*"'控件

PS:如果您的表单上的“过滤器”控件始终具有相同的语法(例如“search_fieldName”,其中“fieldName”对应于基础记录集中的字段)并且始终位于同一区域(例如 formHeader),然后可以编写一个自动为当前表单生成过滤器的函数。然后可以将此过滤器设置为表单过滤器,或用于其他用途:

For each ctl in myForm.section(acHeader).controls
    if ctl.name like "search_"
        fld = myForm.recordset.fields(mid(ctl.name,8))
        if not isnull(ctl.value) then
           whereClause = whereClause & buildCriteria(fld.name ,fld.type, ctl.value) & " AND "
        endif
    endif
next ctl
if len(whereClause)> 0 then ...
于 2008-10-22T09:09:12.393 回答
2

发生了两件事 - 应该颠倒比较,并且您没有正确引用字符串。

它应该是[数据库字段],如“部分字符串+通配符”

并且所有字符串都需要用引号引起来-不确定为什么您的查询不会引发错误

所以以下应该工作:

,[customerforname] Like  """" & [Forms]![FrmSearchCustomer]![SearchFore] & "*""" )=True

请注意 """" 这是将单个双引号附加到字符串的唯一方法。

于 2008-10-21T22:16:34.317 回答
0

我唯一的想法是可能需要一个 () 来分组类似

例如第一部分的片段

,[Forms]![FrmSearchCustomer]![SearchFore] Like ([customerforname] & "*"))=True

自从我使用访问以来已经有一段时间了,但这是我想到的第一件事

于 2008-10-21T21:54:04.253 回答
0

这是一个完整的重写,以允许名称字段或出生日期字段中的空值。如果在数字 customerid 字段中输入文本,此查询不会因为过于复杂而失败。

SELECT TabCustomers.CustomerForname AS NameSearch, TabCustomers.CustomerSurname AS SurnameSearch, TabCustomers.CustomerDOB AS DOBSearch, TabCustomers.customerid AS MemberSearch
FROM TabCustomers
WHERE TabCustomers.customerid Like IIf([Forms]![FrmSearchCustomer].[Searchmember] Is Null,"*",[Forms]![FrmSearchCustomer]![Searchmember])
AND Trim(TabCustomers.CustomerForname & "") Like IIf([Forms]![FrmSearchCustomer].[SearchFore] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchFore] & "*")
AND Trim(TabCustomers.CustomerSurname & "") like IIf([Forms]![FrmSearchCustomer].[Searchlast] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchLast] & "*")
AND (TabCustomers.CustomerDOB Like IIf([Forms]![FrmSearchCustomer].[SearchDate] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchDate] ) Or TabCustomers.CustomerDOB Is Null)
于 2008-10-21T22:09:58.807 回答