Help with If..or..or..Then...End If logic

K

kw_uh97

Hello everyone I am programming novice so be gentle. I have an if statement
that I would like to alter. The original If ..Then.. End If:

If Not CBool(vRow("Column1")) Or vRow("Column2") = "Answered" Or
vRow("Column4") = "Approved" Then
Do somthing here
Else
Do something else here
End If

However I would like to add an "And" to one of the Conditions that are
checked together like this:

If Not CBool(vRow("Column1")) Or (vRow("Column2") = "Answered" And
CBool(vRow("Column3"))) Or vRow("Column4") = "Approved" Then
Do somthing here
Else
Do something else here
End If


If anyone can provide the syntax or point in the right direction like a URL
I would really appreaciate it. Thanks in advance for any advice.
kwuh97
 
F

Family Tree Mike

Hello everyone I am programming novice so be gentle. I have an if statement
that I would like to alter. The original If ..Then.. End If:

If Not CBool(vRow("Column1")) Or vRow("Column2") = "Answered" Or
vRow("Column4") = "Approved" Then
Do somthing here
Else
Do something else here
End If

However I would like to add an "And" to one of the Conditions that are
checked together like this:

If Not CBool(vRow("Column1")) Or (vRow("Column2") = "Answered" And
CBool(vRow("Column3"))) Or vRow("Column4") = "Approved" Then
Do somthing here
Else
Do something else here
End If


If anyone can provide the syntax or point in the right direction like a URL
I would really appreaciate it. Thanks in advance for any advice.
kwuh97

I'm a little confused, as what you wrote for what you would like to do
is perfectly fine. I would advise though that it might be better to
understand (maintain) your code if it were set up as this:

dim FirstCondition as boolean = Not CBool(vRow("Column1"))
dim AnsweredCondition as boolean = vRow("Column2") = "Answered"
dim ThirdCondition as boolean = CBool(vRow("Column3"))
dim ApprovedCondition as boolean = vRow("Column4")) = "Approved"

if (FirstCondition or AnsweredCondition or ApprovedCondition) then
else
end if

and the new code is

if (FirstCondition or _
(AnsweredCondition and ThirdCondition) or _
ApprovedCondition) then
else
endif


Obviously FirstCondition and ThirdCondition could be named something
more meaningful in your code's context.
 
J

J.B. Moreno

kw_uh97 said:
Hello everyone I am programming novice so be gentle. I have an if statement
that I would like to alter. The original If ..Then.. End If:

If Not CBool(vRow("Column1")) Or vRow("Column2") = "Answered" Or
vRow("Column4") = "Approved" Then
Do somthing here
Else
Do something else here
End If

However I would like to add an "And" to one of the Conditions that are
checked together like this:

If Not CBool(vRow("Column1")) Or (vRow("Column2") = "Answered" And
CBool(vRow("Column3"))) Or vRow("Column4") = "Approved" Then
Do somthing here
Else
Do something else here
End If


If anyone can provide the syntax or point in the right direction like a URL
I would really appreaciate it. Thanks in advance for any advice.

Parenthesis are how you group conditions.

Here's a couple of examples...

If Not a OR b or C then
end if

if Not a OR (b and d) OR C then
end if

if (Not A OR b or C) And (d) then
end if
 
K

kw_uh97

Great!!!! Both replies were very helpful. I wasn't sure if it was the proper
syntax. I guess I lack confidence in my programming. Whenever I did my
debugging I could never get values to confirm the logic.

Thanks Again
kw
 
J

Jack Jackson

One other thing, use AndAlso instead of And and OrElse instead of Or.

And and Or execute all of the tests. AndAlso and OrElse stop when the
other tests could not affect the result. It is more efficient not to
execure useless code, and it sometimes allows you to combine tests in
one If that might otherwise need to be in different statements.

This statement:

If aa Is Not Nothing And aa.Size > 0

will trap is aa is Nothing because aa.Size will always be executed.
With:

If aa Is Not Nothing AndAlso aa.Size > 0

it will not trap, because aa.Size will only be evaluated if aa is not
Nothing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top