Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If fuction with multiple if's issues 1

Status
Not open for further replies.

ViperInc

Programmer
Mar 13, 2001
24
0
0
AU
Hi.

I'm having a really stange error.
Im using the KeyAscii function to read buttons pressed from a remote control(it works, no need to go into details). In the program I'm setting these buttons to perform certain tasks. (mainly to use with winamp, play, stop, next, etc.)
The prblem arrises when I try to check that no button has been assigned twice.
I thought I could use the 'If' fucntion then use 'Or' to specify if a button has been used more than once
eg.

PauseBut = KeyAscii
If PauseBut = PrevBut Or PlayBut Then
MsgBox "Already Assigned This Button."
Exit Sub
End If

But no matter what the 'pausebut' value is, it will get a positive hit from the 'If' statement.

Am i usuing the 'Or' statement incorrectly???
or what is the problem???

thanks

Ben
 
Your code
If PauseBut = PrevBut Or PlayBut Then


will give true for any non-zero value for playbut

You need If PauseBut = PrevBut Or PauseBut = PlayBut Then

The 'or' is a logic 'or' not a grammatical 'or' so it looks at the values of the 2 expressions either side. In your case one expression is 'If pausebut = Prevbut' and the other expression is Playbut.

Normal logic rules (as used by 'Or') take 0 as false and any non-zero value as true. Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
What is happening is that the two expression in the if statement (PauseBut = PrevBut) and (PlayBut) are being logically OR'ed together. Since PlayBut has a value, it is returning True, and thus the ORing of these two expressions is returning True.

I suspect that what you want to do is to for your two expressions to be (PauseBut = PrevBut) and (PauseBut = PlayBut), and for that, the correct syntax would be

If ((PauseBut = PrevBut) Or (PauseBut = PlayBut)) Then

The Parens are not necessary, but to remove any question and ambiguity, I always fully parenthesize compound conditionals. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Ah, thanks guys...
I thought i might be something simple like that...


Coming from a physics background, the OR to me, would hav meaned either one OR the other... but obviously VB doesnt like being treated this way...

thanks again!
 
ViperInc,

Did you find either of your answers either helpful or expert?

I notice that you have asked 10 questions in this forum, but not marked any as helpful! Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top