Search code examples
vb6textboxnumbers

Only allow numeric values in the textbox


I want to make a TextBox control that only accepts numerical values.

How can I do this in VB6?


Solution

  • In the text box text Change event, check if the entered value is a number. If it's not a number then set the old value back again.

    Dim textval As String
    Dim numval As String
    
    Private Sub TextBox1_Change()
      textval = TextBox1.Text
      If IsNumeric(textval) Then
        numval = textval
      Else
        TextBox1.Text = CStr(numval)
      End If
    End Sub