当前位置:  开发笔记 > 编程语言 > 正文

返回部分匹配值的记录

如何解决《返回部分匹配值的记录》经验,为你挑选了2个好方法。

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

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

Godeke.. 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的记录.



1> Godeke..:

你的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的记录.



2> Philippe Gro..:

有一个访问方法!

如果您在表单上有"过滤器"控件,为什么不使用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 ...

推荐阅读
郑谊099_448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有