if condition

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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")
 
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")
 
Back
Top