"David Gray" <bl**@blah.co m> wrote in message
news:9m******** *************** *********@4ax.c om...
Ok The fields are all individual text boxes and I need
to validate each one is within a range of values.
Personally I prefer to allow the user to enter whatever he wants and then
analyse and interpret it only when he leaves the text box, displaying the
results of the interpretation in the text box. However, if you want to
prevent him from entering anything other than digits in the first place (as
you say you want to do) then there are various ways to accomplish that.
Using the API to set the Style to ES_NUMBERS (or whatever it is called)
doesn't do what you want, because it still allows the user to paste other
stuff in, so you will need to write a bit of code.
Here is an example that checks for the Shift or Ctrl keys in the KeyDown
event (before the keypress event occurs) and sets a flag (using the Tag
property) if one or other is held down. The KeyPress event then checks the
flag and rejects the keypress if Ctrl or Shift were held down (thereby
preventing Ctrl V and Shift Insert). It then checks for a digit (0 to 9). As
it stands the code will reject all attempts to paste stuff in (using Ctrl V
or Shift Insert), including attempts to paste in digits. If you want to
reject "pasted" stuff and yet still accept "pasted digits" then that, too,
can be easily accomplished with a little bit of extra code.
By the way, you say that you will use the IsDate function to check the
validity of the date when you finally construct it from the threee text box
inputs. If so, then make sure that you ask the user to enter all four digits
for the year. That "IsDate" function accepts all sorts of rubbish,
especially when the year is specified using just two digits.
Print IsDate("32/08/03")
I apologise to Martin for diving in on this one, but if he will insist on
sitting back in Trump Towers quaffing brandy then he deserves all he gets
;-)
Code follows:
Mike
Private Sub Text1_KeyDown(K eyCode As Integer, Shift As Integer)
If Shift <> 0 Then
KeyCode = 0
Text1.Tag = "reject"
Else
Text1.Tag = ""
End If
End Sub
Private Sub Text1_KeyPress( KeyAscii As Integer)
If Text1.Tag = "reject" Then KeyAscii = 0
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
KeyAscii = 0
End If
End Sub