Solved

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

Posted on 1998-12-16
3
117 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
3 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 20 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

762 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

13 Experts available now in Live!

Get 1:1 Help Now