VBScript Conditional Help

Posted on 2007-03-28
Last Modified: 2008-01-09
I created what I would consider a straight foward conditional statement with multiple conditions but it appears to be much more complex than i expected as it's not returning the correct results.  This is in VBScript so that may be why the logic isn't responding as I would have expected.

dim c200, w200, c500, w500
c200 = CheckRegKSvc(regKvals, "200", "C") 'will return a 1
w200 = CheckRegKSvc(regKvals, "200", "W") 'will return a 0
c500 = CheckRegKSvc(regKvals, "500", "C") 'will return a 1
w500 = CheckRegKSvc(regKvals, "500", "W") 'will return a 0

if  (c200 = 1 or w200 = 1) and _
    (c500 = 1 or w500 = 1) then
      Call msgbox("Yes!")
      Call msgbox("No!")
end if

'--------- the function above responds with a yes

if (CheckRegKSvc(regKvals, "200", "C") = 1 or CheckRegKSvc(regKvals, "200", "W")) = 1 and _
   (CheckRegKSvc(regKvals, "500", "C") = 1 or CheckRegKSvc(regKvals, "500", "W")) = 1 then
      Call msgbox("Yes!")
      Call msgbox("No!")
end if

'------------------this function responds with a no....

the only difference is i set the values to a variable and compare that variable and it responds as expected, when i try to put my function logic in the conditional it responds incorrectly...
Question by:jimtxas
  • 2
LVL 35

Accepted Solution

mvidas earned 500 total points
ID: 18811286
Hi Jim,

I think it has to do with the positioning of your parenthesis. Currently you have the line:
if (CheckRegKSvc(regKvals, "200", "C") = 1 or CheckRegKSvc(regKvals, "200", "W")) = 1 and _
   (CheckRegKSvc(regKvals, "500", "C") = 1 or CheckRegKSvc(regKvals, "500", "W")) = 1 then

Try changing that to:
if (CheckRegKSvc(regKvals, "200", "C") = 1 or CheckRegKSvc(regKvals, "200", "W") = 1) and _
   (CheckRegKSvc(regKvals, "500", "C") = 1 or CheckRegKSvc(regKvals, "500", "W") = 1) then

I moved the parenthesis after the =1 for each of the 2nd conditions in the 'or' blocks.  Give it a try, should work as needed.

Author Comment

ID: 18811324
ouch... you're completly right.  I've been staring at it so long I don't think i would have ever caught that.  Fresh eyes are always good.  Thanks much!
LVL 35

Expert Comment

ID: 18811343
I hate that when you stare at something and work with something for so long that the little (normally easy) things just cant work. It is like writing/typing the same word so many times that it begins looking like it is spelled wrong :)
Let me know if you need anything else!

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now