UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> IsNumbersOnly    

See also: GetDigitsOnly and IsAlphaOnly

CODE
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
' Original submission by Walter Niesz
' July 25, 2010
'
' You are free to use it in any application,
' provided the copyright notice is left unchanged.

' DESCRIPTION
' This function tests a given string to see
' whether it is composed of all numeric digits.

Const AllNumbers = "0123456789"

Public Function IsNumbersOnly(StringToTest As String) As Boolean

   Dim i As Long

   IsNumbersOnly = True
   
   For i = 1 To Len(StringToTest)
       If InStr(AllNumbers, Mid(StringToTest, i, 1)) = 0 Then
           IsNumbersOnly = False
           Exit Function
       End If
   Next i
   
End Function

NOTE: The built-in MS Access function of IsNumeric() is similar, however there are slight differences.

The IsNumeric() function will return True if a character string equates to a number, even if the string contains non-numeric characters.

Ex:

? IsNumeric("-2")

True

? IsNumeric("57.4")

True

? IsNumeric("1E3")

True

? IsNumeric("&H200")

True

? IsNumeric("$10.00")

True

The IsNumbersOnly() function posted above will only return True if the characters are truely all numbers. The list of valid characters can be expanded to include characters such as hyphens, periods, etc, should that be the desired behavior.


Creative Commons License
IsNumbersOnly by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 7,419 times.  This page was last modified 03:35, 6 November 2013 by Walter Niesz. Contributions by Mark Davis and Jack Leach  Disclaimers