Results 1 to 3 of 3

Thread: MaskEd Boxes Please Help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Location
    Ireland
    Posts
    5

    Thumbs up MaskEd Boxes Please Help!

    I have added some MaskEd Boxes onto a form in a vb project. One of them is Age.

    I need to put an age restriction not to let a person continue unless they are 18 or over.

    This is the code i have put in:

    If mskAge.Text < "18" Then
    MsgBox "Please enter a valid age", vbExclamation, "Age Restriction"
    Else
    frmPersonalDetails.Hide
    frmServices.Show
    End If


    But when i click run and test it, it works if i enter 10,11,12,13,14,15,16,17 but if i try anything less than ten it lets you through to the next page which i dont want!

    Any ideas?

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

    Re: MaskEd Boxes Please Help!

    Welcome to VBForums

    The problem is that you aren't comparing numbers (where 9 < 10), but some characters (where "2" > "111111111111").

    What you should do is convert the value in the MaskEd box to a number and compare that to a number, eg:
    Code:
    If Val(mskAge.Text) < 18 Then
    Note that Val will convert without giving any errors - if the user enters something other than a number (eg: "Hello") then Val will simply return 0, or if there is a number followed by text (eg: "23 Hello") it will return the number.

    An alternative would be to use CInt, which will give an error if there is anything other than a number, eg:
    Code:
    If CInt(mskAge.Text) < 18 Then

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Location
    Ireland
    Posts
    5

    Re: MaskEd Boxes Please Help!

    Ok thanks a million, ill give that a try!!

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