小编典典

返回与值部分匹配的记录

sql

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

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

阅读 215

收藏
2021-03-23

共1个答案

小编典典

有一种访问方法!

如果您在表单上具有“过滤器”控件,那么为什么不使用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 ...
2021-03-23