简体   繁体   中英

If statement with 2 possible numbers

Hey all i am probably overthinking this but how can i check the textbox for either a 655 or 699 in the first 3 numbers in the textbox?

The current code i how now works but displays an error if (im guessing) it doesnt find the other number in the textbox as well:

 If Microsoft.VisualBasic.Mid(txtNumber.Text, 1, 3) <> 655 Or Microsoft.VisualBasic.Mid(txtNumber.Text, 1, 3) <> 699 Then
 'ERROR
 end if

What would i be doing incorrectly?

David

Like so:

If Left(txtNumber.Text, 3) = "655" OrElse Left(txtNumber.Text, 3) = "699" Then 
   ' good?
End if 

Although it looks like you might want an error if it's not either one, in which case just wrap the two test above in paran's and put a Not before them.

First, you're going to want to use Left, not Mid if it's the first 3 characters.

Second, you're checking a string against an integer.

Third, you're checking if it's not those 3 characters when I'm guessing you want to check if they are equal, so you'll want to change that, as well.

try

    If Mid(txtNumber.Text, 1, 3) <> "655" And Mid(txtNumber.Text, 1, 3) <> "699" Then
        'Code
    End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM