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

check for numbers only in text box 1

Status
Not open for further replies.

drctx

IS-IT--Management
May 20, 2003
226
US
i have an unbound text box that only needs to have numbers entered in it like 123.0005.
is there a way to test for only number in the text box?
 
If the entry will always be three digits + decimal + four digits, you can set the Input Mask property:

000\.0000



HTH,
Bob [morning]
 
If you know exactly how you want the data to be (number of digits, etc) then the input mask is best.
Else you may want to check out the following function.

Use this in the on KeyPress event
This allows only numerics into a control. You can, however, append to this function to ensure only one decimal is input, or append even more to ensure if a decimal is input and no prior numbers that a 0 shows up prior to the decimal. Hmm, maybe something for me to work on today?

Code:
Private Sub TextTrial_KeyPress(KeyAscii As Integer)
   KeyAscii = AllowNumericOnly(KeyAscii)
End Sub

Code:
Public Function AllowNumericOnly(ByRef Pintascii As Integer)
    If Pintascii = 8 Then GoTo SetKey  'this is the backspace
    If (Chr(Pintascii) < "0" Or Chr(Pintascii) > "9") And Chr(Pintascii) <> "." Then
        Pintascii = 0
    End If
    
SetKey:
    AllowNumericOnly = Pintascii
End Function
 
thanks Axoliien, that works great and it's better than my loop and select case statement was working on.
 
You may also take a look at the IsNumeric function in the BeforeUpdate event procedure of your textbox.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top