如何制作 if 语句以停止从 运行 查询访问?

How to make an if statement to stop access from running query?

我使用的是 Microsoft Access,我想创建一个基本上可以执行以下操作的 if 语句:

如果搜索条件全部为空,则打开一个显示 "something I write" 的消息框和一个“确定”按钮。好的会让你回到搜索而不是 运行 查询。

我的问题是,如果所有搜索条件都留空并且有人点击搜索,它将导致访问崩溃。所以我想做一些事情来阻止某人 运行 空着查询。我正在使用具有 6 个不同标准的表单,称为 Standards、Duds、ID、Desc1、Desc2 和 Excel。

也想让大家知道,我在编码和其他方面非常愚蠢,所以如果您能以 4 岁的孩子可以理解的方式拼写出来,那就太好了。

我想要这样的东西:

如果 [Forms]![Search]![Standards] AND [Forms]![Search]![CADID] 为空则

MsgBox("You cant do this") - 然后这会让您回到表单

ElseIf

运行查询正常

目前我的代码看起来有点像:

SELECT DISTINCT Standards.Name, Standards.[Catalog Id], - 然后是一堆其他表及其各自的列

WHERE(((Standards.Name)Like"*"& [Forms]![Search]![Standards] & "*") AND ((Standards.[Catalog ID]) Like "*" & [Forms]![Search]![CADID] & "*"));

我不知道为什么到此为止。用户在文本框 WHERE 之后读取 [Standards] 和 [CADID] 的位置,然后写入 form.let 我知道您需要多少详细信息。

不确定您的搜索代码是什么,但这里有一个模板可供使用。将它放在执行搜索的按钮的单击事件中,并确保添加执行搜索的任何代码来代替我在下面 If 语句的其他部分中留下的评论。

If _
    (IsNull([Forms]![Search]![Standards]) Or _
    [Forms]![Search]![Standards] = "") And _
    (IsNull([Forms]![Search]![CADID]) Or _
    [Forms]![Search]![CADID]) = "" _
Then

    MsgBox "Please complete both Standards and CADID fields before searching", vbCritical Or vbOKOnly, "Search Error"

Else

    ' your search code here

End If