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

IE。在姓氏框中,我应该能够输入 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;
有帮助吗?

解决方案

有一个访问方法!

如果表单上有“过滤器”控件,为什么不使用 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*"' 在控制中

附:如果表单上的“过滤器”控件始终具有相同的语法(假设“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 ...

其他提示

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

正在进行两件事 - 比较应该颠倒过来,而你没有正确引用字符串。

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

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

所以以下内容应该有效:

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

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

我唯一的意思是,可能需要a()来分组

例如,第一部分的片段

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

自从我使用了访问权限已经有一段时间了,但这是首先想到的事情

这是一个完整的重写,允许名称字段或出生日期字段中的空值。如果在数字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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top