|
-
Nov 6th, 2008, 06:06 AM
#1
Thread Starter
New Member
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?
-
Nov 6th, 2008, 06:18 AM
#2
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
-
Nov 6th, 2008, 06:22 AM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|