简体   繁体   中英

How to combine multiple If statements/Dlookup functions?

I have the following code that I am running when a button is clicked on a Form:

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"

The code is not throwing any errors, but is not resulting in the expected behavior. I am fairly confident the issue is from combining multiple If/Dlookup functions consecutively. I have only ever coded using one at a time, so I am not sure how to combine these since it is not just multiple criteria of one Dlookup, but multiple Dlookup statements.

Any assistance would be greatly appreciated.

Update 8/5/2018 4:55pm EST First section of code is now working thanks to the suggestions. Still not able to get this portion to work:

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"

No action is occurring at all when the button is clicked. As stated in a comment, PCP is a Yes/No field, the GWSHW BL is a five digit identifier (1.2.0.0.01), and GWS HW HW is a three letter acronym (ABC).

DLookup will return either a value or if no match Null. So the If needs to consider Null in conditional test:

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

or

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

Do not use apostrophe delimiters for field names. And probably should concatentate form control references. Reference the subform container control, not name of form it holds:

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

If ECN is a text type field, use apostrophe delimiters on the parameter:

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

Shortcut use Me.:

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

Recommend naming subform container different from the object it holds, such as ctrStatus, then:

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

Those examples reference a field name. If you want to reference a control named like tbxECN:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM