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
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
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) <> 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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.