Using And/Or within If statement

randolphoralph

Registered User.
Local time
Today, 07:53
Joined
Aug 4, 2008
Messages
101
I am having some issues trying to use both and & or in a If statement.

The code below is what I am trying to do and it is not working.

Code:
ElseIf (Len(Nz(Me.Associate_name, "")) = 0 And Me.Status <> "Expired") Or (Len(Nz(Me.Associate_name, "")) = 0 And Me.Status <> "No longer eligible") Then
   MsgBox "Please enter Associate Name"
   Me.Date_authorization_expires.SetFocus

I tried the code below and it works but as soon as a I enter the Or part it will not work.

Code:
ElseIf (Len(Nz(Me.Associate_name, "")) = 0 And Me.Status <> "Expired") Then
   MsgBox "Please enter Associate Name"
   Me.Date_authorization_expires.SetFocus

I am not sure where I am going wrong.
 
This code looks like it should work. What error are you getting and on what line?
 
Suggestion.

No need for both NZ and Len. Instead of

IF (Len(Nz(Me.Associate_name, "")) = 0 try

IF Len(Me.Associate_name) = 0

Next. Does the second part of your formula work on its own. We know it does not work when added to the first part, so check it on its own.
 
Ok I have tried the second part of the formula and it works as shown below. I am not getting an error message when I use both parts of the formula, but the issue is that the code will return the message box regardless of field values.

Code:
ElseIf (Len(Nz(Me.Associate_name, "")) = 0 And Me.Status <> "No longer eligible") Then
   MsgBox "Please enter Associate Name"
   Me.Date_authorization_expires.SetFocus

I will remove the NZ as suggested.
 
After removing the Nz neither part of the formula works. I am ok with leaving the Nz as is, but just have to figure out how to make the 2 parts work together.
 
I think that this is what you are looking for:
Code:
ElseIf (Len(Nz(Me.Associate_name, "")) = 0) And (Me.Status <> "No longer eligible") Then
Remember the order of operations in math: First you perform X then +
3 X 2 + 1 = 7
In boolean logique AND is performed first and OR is the second
AND -> X
OR -> +

And the brackets change this order as you need.
3 X (2 + 1) = 9
 
I forgot to say that is a very good practice to use brackets when you write boolean formulas in order to encapsulate the logic items.
 
Mihail I tried as you suggested on both parts of the formula and it works when I use only one part, but as soon as I add them together with the OR it does not work.

Code:
ElseIf (Len(Nz(Me.Associate_name, "")) = 0) And (Me.Status <> "Expired") Or (Len(Nz(Me.Associate_name, "")) = 0) And (Me.Status <> "No longer eligible") Then
 
I have huge doubts but try this:
Code:
ElseIf ((Len(Nz(Me.Associate_name, "")) = 0) And (Me.Status <> "Expired")) Or ((Len(Nz(Me.Associate_name, "")) = 0) And (Me.Status <> "No longer eligible")) Then
 
I am having some issues trying to use both and & or in a If statement.

The code below is what I am trying to do and it is not working.


ElseIf (1Len(2Nz(3Me.Associate_name, ""2)1) = 0 And Me.Status <> "Expired"0) Or (1Len(2Nz(3Me.Associate_name, ""2)1) = 0 And Me.Status <> "No longer eligible"0) Then
MsgBox "Please enter Associate Name"
Me.Date_authorization_expires.SetFocus


At 0, your elseif ends. The Or then becomes it's own statement, outside of the elseif statement. Weird things are bound to happen, because it's likely the OR was meant to be included in the IF.
 
Ok I feel so dumb....I found out what the issue is....I should have been using AND instead of OR.

Below is the code that ended up working.

Code:
[COLOR=#000000][FONT=Segoe UI]ElseIf (Len(Nz(Me.Associate_name, "")) = 0)
 and (Me.Status <> "Expired") 
 and (Me.Status <> "No longer eligible") Then [/FONT][/COLOR]

I get very confused when to use AND versus OR.
 
OR table
True OR True = True
True OR False = False OR True = True
False OR False = False
I'll drink a beer IF (I have money) OR (you give me one)

AND table
True AND True = True
True AND False = False AND True = False
False AND False = False
I'll drink a beer IF (I have money) AND (I have time)

I'll drink a beer IF (I have money OR you give me one) AND (I have time)
This mean that if I have time = False I'll NOT drink that beer even I have money and you say that you give me a beer.

Do you say that ?!?!? :eek:
 

Users who are viewing this thread

Back
Top Bottom