Click to See Complete Forum and Search --> : Textbox for only numerical input including -ve values


Scott Johnston
January 13th, 2000, 03:59 PM
I am trying to filter the input to a textbox such that:

1. Only numerical values will be accepted.
2. A decimal point will only be allowed once.
3. Negative values will be allowed but only with
the -ve sign at the beginning of the input and
only being accepted once.

I have tried the DataFormat property and this does not seem to work. I seem to be able to trap the text values and decimal point constraints but the -ve has escaped me so far...

Any suggestions???

I am quite new to VB and may have missed something fundamental. Perhaps there is an API Function angle to this one with which I am not familiar...

Thanks

johnpc1
January 13th, 2000, 04:25 PM
Here is most of what your looking for; you should
be able to figure the rest out, i.e. -ve, only once etc.,(vbsubract is the - key, which is ascii 109,I think)just set up a trap or counter for it

public Sub NumbersOnly(KeyAscii as Integer)
'***********************************************************
'* Subroutine: NumbersOnly
'* Description: Permits only numbers,
'* Delete Key, Backspace Key and Enter Key to be input
'* to the control calling this function
'***********************************************************

Select Case KeyAscii
Case vbKeyDelete 'allow Delete Key
Case vbKeyBack 'allow Backspace Key
Case vbKeyReturn 'allow Enter Key
KeyAscii = 0
SendKeys "{TAB}"
Case 48 to 57 'allow Numeric Keys
Case else
Beep
KeyAscii = 0
End Select
End Sub

'to call the routine
NumbersOnly KeyAscii




another method using Instring:


'Place a Text Box on a Form add the following to
'it's KeyPress Event

private Sub Text1_KeyPress(KeyAscii as Integer)
If InStr("0123456789-.", Chr(KeyAscii)) = 0 And _
KeyAscii <> vbKeyBack then KeyAscii = 0
End Sub

January 13th, 2000, 08:30 PM
I am not sure how to do it to a inputbox but i do to a textbox:
If Not IsNumeric(text1.Text) Then
MsgBox "Numbers Only!!!"
End If