'How do I use Not in a If statement with more than one condition
Working on a old legacy code using VB or delphi, do not know for sure. I am trying to write-- if itemA does not equal 111 OR itemA does not equal 222 then future date cannot be entered if these itemaA 111 and 222 are enterend, then future date will be excepted. If itemA equals 333,444,123, etc and the date entry is in future date, it will not allow it. The syntax is wrong somewhere.
If NOT (itemA = 111) or not (itemA = 222) then
if dateEntered > Date then
Dialog ('Future date not allowed for item.');
end if
end if
Solution 1:[1]
Got it solved.
Instead of
if not (itemA = 111) or not (itemA=222) then...
It worked with
if not ((itemA = 111) or (itemA=222)) then ...
The parentheses did the trick
Solution 2:[2]
The VB code, from what you have written, would look something like:
If itemA <> 111 Or itemA <> 222 Then
...
End If
Or, alternatively, and perhaps more clearly:
If Not (itemA = 111 And itemA = 222) Then
...
End If
However, neither of these options makes sense as the condition always resolves to True: itemA
cannot simultaneously equal 111 and 222. The correct logic should be the other way around, eg:
If itemA <> 111 And itemA <> 222 Then
...
End If
But I don't think what you're actually after is VB code.
Solution 3:[3]
I think the original logic is wrong. I mean you can do this in vb:
If Not (itemA = 111) Or Not (itemA = 222) Then
If dateEntered > Date.Today Then
MsgBox("Future date not allowed for item.")
Exit Sub
End If
End If
But I don't think that is or was original correct. The above if will ALWAYS be true since one or the other always going to occur?
I think you need this:
If Not (itemA = 111) And Not (itemA = 222) Then
If dateEntered > Date.Today Then
MsgBox("Future date not allowed for item.")
Exit Sub
End If
End If
The above is a 1 to 1 conversion here. (and yes that does look like delphi).
As noted, one could perhaps write this as :
If (itemA <> 111) And (itemA <> 222) Then
If dateEntered > Date.Today Then
MsgBox("Future date not allowed for item.")
Exit Sub
End If
End If
But as noted using "or" for this does not seem to make sense. It's possible that delphi does or did a bit-wise OR - but that still does not seem to make much sense.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | WeJava |
Solution 2 | SteveCinq |
Solution 3 | Albert D. Kallal |