OzzieBloke
Programmer
I have a text box which strictly requires a numerical value. If any character other than a number is entered, the program performs an error. Is there a way to prevent the user from entering anything other than a number??
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Private m_strUndo As String 'modular variables
Private m_bolControl As Boolean
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) And Text1.Text <> "" Then
Text1.Text = m_strUndo
Text1.SelStart = Len(Text1.Text)
Beep
Else
m_strUndo = Text1.Text
End If
End Sub
Private Sub Text1_GotFocus()
m_strUndo = Text1.Text
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyControl Then m_bolControl = True
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
'// Catch values entered from keyboard.
If KeyAscii = 46 Then 'allow one decimal.
If InStr(Text1.Text, ".") Then KeyAscii = 0
ElseIf KeyAscii = 22 Then 'Cut and paste with keyboard from the clipboard.
If Not (m_bolControl And IsNumeric(Clipboard.GetText)) Then KeyAscii = 0
ElseIf Not ((KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = 8) Then
KeyAscii = 0
End If
If KeyAscii = 0 Then Beep 'provide some feedback
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
m_bolControl = False
End Sub