如何组合多个If语句/ Dlookup函数?

时间:2018-08-05 19:41:31

标签: vba ms-access if-statement access-vba if-this-then-that

在窗体上单击按钮时,我正在运行以下代码:

If DLookup("ECN_Class_I_Change", "ENGINEERING_CHANGE_NOTICE_TABLE", "[ECN_Class_I_Change] = -1") Then
If DLookup("ECN", "ENGINEERING_CHANGE_NOTICE_TABLE", "'ECN' = 'Forms!DRAWING_INFO_FORM!Subform_DRAWING_STATUS!Subform_DRAWING_STATUS_ECN!ECN'") Then
    strMessage = "Change third digit."
    MsgBox strMessage, vbInformation, "Action Required"

    Else

 If DLookup("PCP", "REVIEW_PANEL_TABLE", "[PCP] = -1") Then
 If DLookup("GWSHW_BL", "REVIEW_PANEL_TABLE", "'[GWSHW_BL]' = 'Forms!DRAWING_INFO_FORM!Subform_DRAWING_STATUS!Subform_DRAWING_STATUS_CONFIGURATION_HWCI_BL!BL_HWCI_HWCI'") Then
 If DLookup("GWSHW_HW", "REVIEW_PANEL_TABLE", "'[GWSHW_HW]' = 'Forms!DRAWING_INFO_FORM!Subform_DRAWING_STATUS!Subform_DRAWING_STATUS_CONFIGURATION_HWCI_BL!BL_HWCI_HWCI'") Then
    strMessage = "Change fourth digit - HWCI/BL has been certified."
    MsgBox strMessage, vbInformation, "Action Required"

        Else
          strMessage = "Change fifth digit"
          MsgBox strMessage, vbInformation, "Action Required"

该代码不会引发任何错误,但不会导致预期的行为。我非常有信心,问题是来自连续组合多个If / Dlookup函数。我一次只使用一个编码,所以我不确定如何将它们组合在一起,因为它不仅是一个Dlookup的多个条件,而且是多个Dlookup语句。

任何帮助将不胜感激。

更新8/5/2018 4:55 pm EST 有了这些建议,第一部分代码现在可以正常工作了。仍然无法使这部分起作用:

If Not IsNull(DLookup("PCP", "REVIEW_PANEL_TABLE", "[PCP] = -1")) Then
If Not IsNull(DLookup("GWSHW_BL", "REVIEW_PANEL_TABLE", "GWSHW_BL = " & Me.Subform_DRAWING_STATUS!Subform_DRAWING_STATUS_CONFIGURATION_HWCI_BL!BL_HWCI_BL)) Then
If Not IsNull(DLookup("GWSHW_HW", "REVIEW_PANEL_TABLE", "GWSHW_HW = '" & Me.Subform_DRAWING_STATUS!Subform_DRAWING_STATUS_CONFIGURATION_HWCI_BL!BL_HWCI_HWCI & "'")) Then
   strMessage = "Change fourth digit - HWCI/BL has been certified."
   MsgBox strMessage, vbInformation, "Action Required"

      Else

        strMessage = "Change fifth digit"
        MsgBox strMessage, vbInformation, "Action Required"

单击按钮时根本不执行任何操作。如评论中所述,PCP是“是/否”字段,GWSHW BL是五位数字标识符(1.2.0.0.01),而GWS HW HW是三字母缩写(ABC)。

1 个答案:

答案 0 :(得分:1)

DLookup将返回一个值,或者如果不匹配则返回Null。因此,如果在条件测试中需要考虑Null:

If IsNull(DLookup("PCP", "REVIEW_PANEL_TABLE", "[PCP] = -1")) Then

If Not IsNull(DLookup("PCP", "REVIEW_PANEL_TABLE", "[PCP] = -1")) Then

请勿对字段名称使用撇号定界符。并且可能应该包含表单控件引用。引用子表单容器控件,而不是它所保存的表单名称:

If DLookup("ECN", "ENGINEERING_CHANGE_NOTICE_TABLE", "ECN = " & Forms!DRAWING_INFO_FORM!Subform_DRAWING_STATUS!ECN)

如果ECN是文本类型字段,请在参数上使用撇号定界符:

If DLookup("ECN", "ENGINEERING_CHANGE_NOTICE_TABLE", "ECN = '" & Forms!DRAWING_INFO_FORM!Subform_DRAWING_STATUS!ECN & "'")

快捷方式使用我:

If DLookup("ECN", "ENGINEERING_CHANGE_NOTICE_TABLE", "ECN = '" & Me.Subform_DRAWING_STATUS!ECN & "'")

建议命名与其保存的对象不同的子窗体容器,例如ctrStatus,然后:

If DLookup("ECN", "ENGINEERING_CHANGE_NOTICE_TABLE", "ECN = '" & Me.ctrStatus!ECN & "'")

这些示例引用一个字段名称。如果要引用名为tbxECN的控件:

If DLookup("ECN", "ENGINEERING_CHANGE_NOTICE_TABLE", "ECN = '" & Me.ctrStatus.Form.tbxECN & "'")