MS Access:留空的情况下,“类似” *“&_____”标准可防止后续字段的标准操作。请指教

时间:2019-03-01 20:29:31

标签: sql ms-access ms-access-2010

我在MS Access中创建了一个查询,该查询引用了表单中的文本,以创建表达式的条件来按道路名称或地区在表中查找记录。

其他人最终将使用该表格,因此我使用like通配符来灵活地在表格中输入不完整的道路名称。

不幸的是,这意味着如果将道路名称文本框留为空白,则like函数将显示数据库中的所有记录,并且不会基于第二个标准(地区名称)对其进行限制。 / p>

我应该使用其他函数还是编写更复杂的条件?

(我曾尝试删除通配符,将Or函数放在不同行或同一行的每个字段中,并且还考虑过添加到现有宏中,以通过区名限制查询结果,如果街道名称文本框留为空白。)

我已经使用谷歌搜索了很长时间,无法解决这个问题。谢谢您的帮助!

本质上: 表单(称为MJidea)具有两个文本输入框-

  • 街道(PriStReport
  • 区(District

查询设置:

Screenshot of current query set up

1 个答案:

答案 0 :(得分:0)

您可以将查询的SQL where子句更改为:

where
(
    [Forms]![MJidea]![PriStReport] is not null and
    [Primary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
    [Forms]![MJidea]![PriStReport] is not null and 
    [Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
) or
(
    [Forms]![MJidea]![District] is not null and 
    [Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)

或者,合并前两个测试:

where
(
    [Forms]![MJidea]![PriStReport] is not null and 
    (
        [Primary Street]   like "*" & [Forms]![MJidea]![PriStReport] & "*" or 
        [Secondary Street] like "*" & [Forms]![MJidea]![PriStReport] & "*"
    )
) 
or
(
    [Forms]![MJidea]![District] is not null and 
    [Magisterial District] like "*" & [Forms]![MJidea]![PriStReport] & "*"
)