Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

'IF' with 'AND'/'OR' ...

Posted on 1998-12-16
3
Medium Priority
?
123 Views
Last Modified: 2010-05-03
This is VERY ODD...

pos = instr(1,"HELLO WORLD","XX")

'pos will = 0 since "XX" wont be found in it
if pos > 0 or (pos > 0 and mid("HELLO WORLD",pos)="WORLD") then
   'process whatever
endif

The if statement above is ILLEGAL in VB because eventhough i have a "pos > 0" for the second condition in the "if" statement it still processes the entire condition making the "mid" function invalid since pos cannot be < 1:
ILLEGAL: mid("HELLO WORLD",0)="WORLD")

This is correct but since i had the "pos > 0" at the beginning of the 2nd condtion it should not have allowed the rest of the condition to be processed thus preventing the mid() function from returning an error. I have NEVER seen any other programming language do this before. It is like vb is checking the validity of the ENTIRE line before it begins to process it.  Really ODD!  Is there a way around this, preferably keeping my entire conditions all in one "if" statement?



0
Comment
Question by:ljaques
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 80 total points
ID: 1450591
It's a compiler. Yes, it's checking the validity of the entire line.

Break it out into two statements.

M
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1450592
Let me say, friend Mark, the problem is not due to the fact that "it's a compiler", because some other compilers have an option named "boolean short-circuit" (like Pascal, or antique Turbo Basic) in which, when there's no doubt about the final result of a logical expression, program stops evaluating the rest of it,  and branches accordingly. In Ljaques' example, when Pos > 0, it would suffice to execute the "then" portion of the IF, without seeing what happens with the rest, so skipping the potential error of MID$()

Friend Ljaques: unfortunately, this is one of the ton of stupid things Microstuff did when developed VB. The most atonishing thing I've found is the unability to use the "#" char in Format$(), as it worked since decades ago...   ~~~>:(
0
 
LVL 1

Expert Comment

by:muffinthedog
ID: 1450593
ljaques...
crummy hack but will keep it on one line
If pos > 0 Or (Mid("HELLO WORLD", IIf(pos, pos, 1)) = "WORLD") Then
   'process whatever
End If

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

609 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