Iff statement (1 Viewer)

Cronk

Registered User.
Local time
Today, 23:14
Joined
Jul 4, 2013
Messages
2,772
Re #19 how did the extra Else suddenly appear? That's where the current grief is coming from
Code:
If [Status] = "Closed" Then
    
        If Len([Action Taken] & "") = 0 Then
            strMsg = "Action Taken is mandatory " & vbCrLf
            Cancel = True
        End If
        [COLOR=Red][B]Else[/B][/COLOR]
        If Nz([Date closed], 0) = 0 Then
            strMsg = "Date Closed is mandatory"
            Cancel = True
        End If
        If Cancel = True Then
            MsgBox strMsg
        End If
    End If
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:14
Joined
Feb 19, 2002
Messages
43,266
Having status is usually not redundant so I don't object to it in principle. At the moment, there seems to be only Open and Closed as options but it is quite likely that a third or forth or more options might actually be needed.

I also fixed some other problems with the logic.

rainbows,
I guess you didn't understand my comment so I'll code it for you. When you don't understand some instruction, you might want to say that you don't understand. We don't know what your skill level is so we don't know how much we have to do for you and how much you can do for yourself given the direction.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

Dim strMsg As String
    strMsg = ""
    If [Status] = "Closed" Then
        If Len([Action Taken]) = 0 Then
            strMsg = "Action Taken is mandatory " & vbCrLf
            Cancel = True
        End If       
        If Nz([Date closed], 0) = 0 Then
            strMsg = strMsg & vbCrLf & "Date Closed is mandatory"
            Cancel = True
        End If
        If Cancel = True Then
            MsgBox strMsg
            Exit Sub
        End If
    Else
        If Len([Action Taken]) <> 0 Then
            strMsg = "Action Taken is not allowed unless status is Closed " & vbCrLf
            Cancel = True
        End If       
        If Nz([Date closed], 0) <> 0 Then
            strMsg = strMsg & vbCrLf & "Date Closed is is not allowed unless status is closed"
            Cancel = True
        End If
        If Cancel = True Then
            MsgBox strMsg
            Exit Sub
        End If
    End If 
End Sub

If at some point you decide that you need more than two status options, change the outer IF to a Select Case. It will be easier to read and understand.
 

Users who are viewing this thread

Top Bottom