About   -   Contact   -   Purchase   -   Search   -   What's New

Numeric Validation Function
Here's a validation function used to verify numeric values. The code checks for null values, non-numeric values and negative values and returns an identifier, to report the level of success and provide a means to generate appropriate error messages. The following code should be added as a form or standard module function:
Public Function ValidateNum(CheckValue As String) As Integer
'<DESC>		Validates numeric values passed
'		to function</DESC>
'<RETURN>	Integer value pertaining to level of 
'		success</RETURN>
'<ACCESS>	Public
'<ARGS>		CheckValue: Numeric value for interrogation
'<USAGE>	See below:</USAGE>

'0 - OK
'1 - no value
'2 - non numeric value
'3 - negative value
'4 - unknown error

On Error GoTo ErrorHandler

If CheckValue = "" Then
    ValidateNum = 1
    Exit Function
End If

If Val(CheckValue) <> CheckValue Then
    ValidateNum = 2
    Exit Function
End If
If CheckValue < 0 Then 
    ValidateNum = 3 
    Exit Function 
End If 

ValidateNum = 0

Exit Function 

If Err.Number = 13 Then 
    ValidateNum = 2 
    ValidateNum = 4 
End If 
End Function

An example of the code used to call the Validation function is shown below:

Private Sub cmdValidate_Click()

Select Case ValidateNum(txtValueEntry.Text)

Case 0
    'passed validation check
Case 1
    Msgbox "No Value Entered"
Case 2
    Msgbox "Non-numeric Value Entered"
Case 3
    Msgbox "Negative Value Entered"
Case 4
    Msgbox "Please Check Value Entered"
End Select

End Sub

Top of Page

Legal Notice

Ken Howe 2011