Results 1 to 6 of 6

Thread: [RESOLVED] inStr Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    5

    Resolved [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

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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"

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: inStr Problem

    If you want to check whether "7422323" is anywhere in the textbox, change >4 to >0

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    5

    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

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    5

    Re: inStr Problem

    Thanks for your help guys i got it workin had to put >0 instead of >4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width