numbers, backspace and delete keys only

ariel81

Registered User.
Local time
Yesterday, 20:55
Joined
Dec 31, 2006
Messages
75
hi, would appreciate if there is any codes to only allow numbers to be entered into a textbox. "backspace, delete" are also allowed, except carriage return key and spacebar.

thank you.
 
You could use a mask set to numeric for the field, enter key will just step to the next field not add anything to the data.

HTH

Peter
 
Hi Ariel81, I use the following to restrict input into unbound text boxes, place the function in a module and call from a keypress event.

Public Function LimitUnboundTextBox(KeyAscii As Integer, TextBoxSizeLimit As Integer, Optional InputType As String = "Optional: Default=All Chars or enter A for Alpha Only, I for Integer Only")

'Author: Allan L Taylor
'
'Purpose: To limit the number of characters/digits that can be entered into a unbound textbox and
' allows the selection of the input type ie All Chars, Alpha Only or Integer Only
'
'Version 1
'
'Example usage:-
'
'Private Sub TextBoxName_KeyPress(KeyAscii As Integer)
'
' LimitTextBoxSize KeyAscii, 12 'Alpha Numeric input by default with max length of 12 chars
' LimitTextBoxSize KeyAscii, 4, "A" 'Alpha input only with max length of 4 chars
' LimitTextBoxSize KeyAscii, 8, "I" 'Integer input only with max length of 8 digits
'
'End Sub

Dim cntActiveControl As Control
Dim txtTextBoxContent As String

On Error GoTo ErrorDetected

'Get active control
Set cntActiveControl = Screen.ActiveControl

'Get the control text
txtTextBoxContent = cntActiveControl.Text

'Check for backspace being pressed
If KeyAscii = 8 Then

Exit Function

End If

If InputType = "I" Then 'Integer only

If KeyAscii < 48 Or KeyAscii > 57 Then

MsgBox "Input is restricted to a numeric integer only (values '0 to 9')", vbOKOnly, "User Input Warning"
KeyAscii = 0

Exit Function

End If

ElseIf InputType = "A" Then 'Alha only

If (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Then

'do nothing

Else

MsgBox "Input is restricted to a Alpha chars only (values 'a to z' or 'A to Z')", vbOKOnly, "User Input Warning"
KeyAscii = 0

Exit Function

End If

End If

'Warn user the last possible character has been entered
If Len(txtTextBoxContent) = TextBoxSizeLimit - 1 Then

Beep

End If

'check length is < max limit
If Len(txtTextBoxContent) < TextBoxSizeLimit Then

'Append typed character into the textbox
txtTextBoxContent = Left(txtTextBoxContent, cntActiveControl.SelStart) 'Find text box insertion point
txtTextBoxContent = txtTextBoxContent & Chr$(KeyAscii) 'Add new char/digit
txtTextBoxContent = txtTextBoxContent & MID(txtTextBoxContent, cntActiveControl.SelStart + 1 + cntActiveControl.SelLength) 'where applicable place remaining text on end of insertion point

Else

MsgBox "Input size is restricted to " & TextBoxSizeLimit & IIf(InputType = "I", " digits", " chars"), vbOKOnly, "User Input Warning"
KeyAscii = 0

End If

Exit Function

ErrorDetected:

MsgBox Err.Number & " - " & Err.Description, vbCritical, "Unexpected Error Occurred, Operation Aborted"

End Function
 

Users who are viewing this thread

Back
Top Bottom