Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation

Status
Not open for further replies.

cg100

Programmer
Jul 3, 2003
4
MU
Hi

I am trying to write a code whereby user can only input integer values in a text box. If user were to input non integer values then the validate event will be set to false and a msg box will be displayed asking user to enter an integer value

Code as follows :
Private Sub Text1_Validate(Cancel As Boolean)
Dim x, y As Integer
For x = 1 To Len(Text1.Text)
For y = 1 To 255
If Chr$(y) = Mid$(Text1.Text, 1, x) Then
If y < 48 Or y > 57 Then
Cancel = True
MsgBox &quot;You must enter an integer&quot;
Text1.Text = &quot;&quot;
End If
End If
Next y
Next x
End Sub

Problem is there’s an error in this code and I can’t seem to figure out what it is.
Say I input character “a” in the text box it will prompt for the msg box
But if I input an integer in front of the character a i.e 2a then the validate event does not reject it. Why ? It would seem that the above code is checking only the first character that appears in the text box and not the rest

Anybody can help out
Thanks
 
There is already a function in VB to detect if a value is a number or not. IsNumeric.

i.e.

If IsNumeric(MyValue) Then
Msgbox &quot;MyValue is a Number&quot;
End If

Robert
 
You could also reject any non-numeric keys:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57
Exit Sub
Case Else
KeyAscii = 0
Beep
End Select
End Sub

but note this does not cover cut&paste


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
First of all, I would certainly take a look at the suggestions made by TheVampire and johnwm as very good alternative approaches.

But to answer your question, the bug in your program is that you have the second and third parameters of the Mid function reversed.

Couple of other points that you might not be aware of: &quot;Dim x, y As Integer&quot; will not dim both x and y as integers. x will be of variant type since no specific type is given, and y will be an integer. The correct method for both to be integers would be &quot;Dim x as integer, y as integer&quot;

Also, it would probably be a bit faster not to loop through the entire ASCII table for each character in the textbox. Using the ASC function will allow you to simply check each character for a valid ASCII Value.


Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top