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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for numeric values

Status
Not open for further replies.

ishikha

Programmer
Mar 22, 2002
36
0
0
IN
hi,

i want to check whether the data entered in the text box is numeric or not how i will check it.if it is not numeric it should give the error to the user


ishikha


 
Hi,

Check out thread222-216458 for some input.
Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
If you are checking before processing an OK.
Code:
Private Sub cmdOK_Click()
  If IsNumeric(Text1.Text) = False
    MsgBox "Invalid Numeric Format"
    Text1.SetFocus
    Exit Sub
  End If
End Sub
 
Hi All,

The code dsi posted works fine ONLY if you have validation on the keypress event i.e. only allow users to type 0-9.

Otherwise your code will fail if the user types "1E1,"
"12,,,", "12." in a textbox.

IsNumeric tests whether an Expression (text in this case) can be evaluated as a number. This means if a String is made up of Letter and Number Characters, and can be interpreted as Hex it will produce True with Isnumeric.

Also changes in Regional settings on different Computers for Number formatting in regards to ',' and '.' will give different results with Isnumeric.

From experience, To test if Text typed in a Textbox is Numeric you need to use code in the Keypress Event as well as code checking the final Text String. E.g.

Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim numeral$

Select Case KeyAscii
Case Asc("0") To Asc("9")
numeral$ = numeral$ + Chr$(KeyAscii)
Case vbKeyBack
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub cmdOK_Click()
If IsNumeric(Text1.Text) = False
MsgBox "Invalid Numeric Format"
Text1.SetFocus
Exit Sub
End If
End Sub


Regards,

Codefish.
 
I Forgot to mention you can create you own Isnumeric Function. I created one to handled English Number Formats:-

Function IsNumber(vNumber As Variant) As Boolean
'----------------------------------------------------------
' Author: Codefish
'
' Date: 22/08/2001
'
' History: IsNumeric returns True if the entire
' expression is recognized as a
' number; otherwise, it returns False.'
'
' Purpose: - Fixes the Problem When Using IsNumeric
' Function "34,55,66,", "3456,34,,,,,56"
' and Other Formats are classed as Numeric
' But ARE NOT!
' Notes:
'
'----------------------------------------------------------

Dim lOccurs As Integer
Dim sString As Variant
Dim n As Integer

'Test Number First Using In-Built IsNumeric Function
If IsNumeric(vNumber) Then
'Comma Bug - Fixed Here!
'Split Returns a zero-based, one-dimensional array
'containing a specified number of substrings.
'Use Comma as the Delimiter to Find Its Occurance
'in the Number.
sString = Split(vNumber, ",")
lOccurs = UBound(sString)

'Check for a Comma in the Number
If lOccurs > 0 Then
'Number Errors Not Picked Up By The IsNumeric
'Function
'1st Digits i.e #, OR ##, or ###,
If Len(sString(0)) > 3 Then GoTo ErrorHandler

'Check for 000s as 1st Digits
If Left(sString(0), 1) = 0 Then GoTo ErrorHandler

'Secondary Digits i.e. ..,###,..
For n = 1 To lOccurs
If Len(sString(n)) <> 3 Then GoTo ErrorHandler
Next
End If

'Return Its a Number
IsNumber = True
Else
'Not a Number - Trapped by IsNumeric Function
IsNumber = False
End If

Exit Function

ErrorHandler:
'Error
IsNumber = False

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top