if condition

M

Me

The following condition isn't doing what I intend

What could be the problem?
I want to see if the file_num 2nd character should be
between a thru' z or '-'

The following statment checks for a thru' z correct, however
it doesn't do the check for '-' even if I enter '-',
it errors out.

....
ElseIf Mid(FILE_NUM, 2, 1) < "A" Or
Mid(FILE_NUM, 2, 1) > "Z" Or
Mid(FILE_NUM, 2, 1) <> "-" Then
Msgbox ("Error")
.....

Thanks for your help!
-M
 
K

Ken Snell [MVP]

Your logic is faulty ... your If statement will fail with "-" character
because that character "-" is less than "A", so the first test is True.

It's easier in this case to test for the desired result and then use NOT to
get the opposite:

ElseIf Not((Mid(FILE_NUM, 2, 1) >= "A" And _
Mid(FILE_NUM, 2, 1) <= "Z") Or _
Mid(FILE_NUM, 2, 1) = "-") Then
Msgbox ("Error")
 
M

Me

Thank you Ken,
-M

Ken Snell said:
Your logic is faulty ... your If statement will fail with "-" character
because that character "-" is less than "A", so the first test is True.

It's easier in this case to test for the desired result and then use NOT to
get the opposite:

ElseIf Not((Mid(FILE_NUM, 2, 1) >= "A" And _
Mid(FILE_NUM, 2, 1) <= "Z") Or _
Mid(FILE_NUM, 2, 1) = "-") Then
Msgbox ("Error")
 

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