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

Numerical value required in text box

Status
Not open for further replies.

OzzieBloke

Programmer
Mar 28, 2004
31
AU
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??
 
Something like this:

Private Sub Text1_LostFocus()
dim Response as string
if not isnumeric(Text1) then
Response = msgbox("Please enter a valid number.", vbexclamation, "Error")
text1.setfocus
else
{do stuff}
end if
End Sub
 
I forgot to mention that you usually need to make an exception for blanks. Even though it's not a number, usually it is acceptable.
 
RoguePoet01 will not allow you to leave unless it is numeric but it will allow you to enter non numeric values. Below is code that I use to ensure only numeric values are entered from the keyboard or via pasting from the mouse or keyboard.

Code:
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


zemp
 
You could use something like this but you still have to worry about users copying and pasting data into the textbox. (I don't take credit for the below code. I think I got it off of this sight somewhere.)

Public Function OnlyNumericKeys(KeyAscii As Integer) As Integer
Select Case KeyAscii
Case 8, 48 To 57 ' allow backspace and digits
Case Else: KeyAscii = 0 ' reject everything else
End Select
OnlyNumericKeys = KeyAscii
End Function

Or you could look into the MaskEdBox control.

Swi
 
Thank you all for your help, I got it working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top