-
[RESOLVED] inStr Problem
Ok im a newb to vb soo forgive me if this is somestupid little error but everytime i use the code below it goes straight to the else option no matter what is entered in to the text box
Code:
If InStr(txtPhone, "7422323") > 4 Then 'When the text is greater than four exectue command below
txtName = "Chris Henry"
txtAddress1 = "243 Field Road"
txtAddress2 = "Sheldon"
txtTown = "Birmingham"
txtPostCode = "B26 4ER"
Else
MsgBox ("Instert Area Code") 'when the text is 3 or lower this message bx will display
End If
-
Re: inStr Problem
InStr is does not let you know if text is greater or longer than some value, it return the position of a string you are looking for.
If txtPhone was "0000007422323" then InStr would be > 4 (because "7422323" is at position 7). If you want to check the length (how many characters) of a string then use Len function.
Also, leaving it up to VB to resolve the default property of a control is not good practice. When assigning text to a Text Box always add the trailing .Text ... txtName.Text = "John Doe"
-
Re: inStr Problem
If you want to check whether "7422323" is anywhere in the textbox, change >4 to >0
-
Re: inStr Problem
Thanks for your reply i need the button to be able to recognise when the user dosnt enter the area code (the 1st 3 digits) and warn them how would i do that
-
Re: inStr Problem
Since the phone number can vary in length, the simplest thing would be to use two separate Text boxes. One for area code and onde for phone number.
-
Re: inStr Problem
Thanks for your help guys i got it workin had to put >0 instead of >4 :bigyello::bigyello::bigyello: