Only Allow Numbers to be Entered...

Discussion in 'AutoCAD' started by arsnik91, Feb 6, 2004.

  1. arsnik91

    arsnik91 Guest

    I have a small Form with four Textboxes. My intent is to have the user enter numbers into the boxes..only numbers. Is there a way to prevent anything other than an Integer from being entered into the Textboxes?

    Would the "Format" command be used here? If so, would I apply it to the Textbox.text field?
     
    arsnik91, Feb 6, 2004
    #1
  2. arsnik91

    JacobD Guest

    Use the KeyPress event of your textbox, if you don't follow what is going on in the code below, look up "Character Codes" in the help file and you should get the idea.

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If (KeyAscii < 48) Or (KeyAscii > 57) Then
    KeyAscii = 0
    MsgBox "Invalid Keystroke"
    End If
    End Sub


    Regards,
    Jacob.
     
    JacobD, Feb 6, 2004
    #2
  3. Here's one way, there are probably better ones....
    In the Form's module, add this code. Be sure to switch TextBox1 to your
    textbox's name. This will let digits and the decimal through, and stop
    anything else. You'll need four copies for each of your textboxes.

    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Is < 46
    KeyAscii = 0
    Case 47
    KeyAscii = 0
    Case Is > 57
    KeyAscii = 0
    End Select
    End Sub


    You can find the ASCII codes for keys either in the Character Map under
    Accessories on the Windows Start menu, or add the following to a form with a
    label and textbox. The label will show the ASCII code of whatever you type
    into the textbox.

    Private Sub TextBox1_Change()
    If Len(TextBox1.Text) > 0 Then
    Label1.Caption = Asc(Left$(TextBox1.Text, 1))
    TextBox1.Text = ""
    End If
    End Sub

    James
     
    James Belshan, Feb 6, 2004
    #3
  4. arsnik91

    arsnik91 Guest

    Thanks you both for your help! Situation solved...
     
    arsnik91, Feb 6, 2004
    #4
  5. Not entirely.

    You now need to contend with Ctrl+V (paste from clipboard).

    In the language I use, I handle the WM_PASTE window message,
    look at what's in the clipboard, and reject it if it contains invalid
    characters.

    However, this isn't simple in VB, because it requires handling
    windows messages of the edit control.
     
    Tony Tanzillo, Feb 7, 2004
    #5
  6. I use the VAL function to filter out non-numeric entries when the user tries
    to change focus away from the textbox. In my case I also needed to prevent
    negative numbers. If they input a negative or zero, the Cancel parameter
    would keep them from exiting the control. Which reminds me, if you want
    negative values allowed, the previous messages will need to be edited to
    allow the ASCII code for "-". This code should handle clipboard pastes.

    Private Sub txtPaperUnits_BeforeUpdate(ByVal Cancel As
    MSForms.ReturnBoolean)

    If Val(txtPaperUnits.Text) <= 0 Then
    MsgBox "Please input a positive number."
    txtPaperUnits.SelStart = 0
    txtPaperUnits.SelLength = Len(txtPaperUnits.Text)
    Cancel = True
    Else
    txtPaperUnits.Text = Val(txtPaperUnits.Text)
    End If

    End Sub
     
    James Belshan, Feb 9, 2004
    #6
  7. Using the change event instead of keypress would catch this.

    Regards - Nathan
     
    Nathan Taylor, Feb 9, 2004
    #7
  8. arsnik91

    TJNJR Guest

    For what it's worth... I had a few troubles with some of the solutions
    I have found around the web for doing this. What if the user presses
    the backspace key? What if they need a negative number? What if they
    need a double (which requires a single decimal character). Anyway, here
    was my solution..

    Public Sub TBox_DoublesOnly(oTextBox As TextBox, ByVal KeyAscii As
    MSForms.ReturnInteger, Optional bAllowNegative As Boolean = False)
    Dim sKeyChar As String
    sKeyChar = Chr(KeyAscii)

    If Not bAllowNegative And sKeyChar = "-" Then GoTo REJECT_KEY
    If (Not IsNumeric(sKeyChar) And Not sKeyChar = "." And Not sKeyChar =
    "-") Then GoTo REJECT_KEY

    If sKeyChar = "." And (InStr(1, oTextBox.Text, ".") > 0) Then GoTo
    REJECT_KEY

    If sKeyChar = "-" Then
    If InStr(1, oTextBox.Text, "-") > 0 And oTextBox.SelLength <>
    Len(oTextBox.Text) Then GoTo REJECT_KEY
    If InStr(1, oTextBox.Text, "-") = 0 And Not oTextBox.SelStart = 0
    Then GoTo REJECT_KEY
    Else
    If InStr(1, oTextBox.Text, "-") > 0 Then
    If oTextBox.SelStart = 0 And oTextBox.SelLength <>
    Len(oTextBox.Text) Then GoTo REJECT_KEY
    End If
    End If

    Exit Sub
    REJECT_KEY:
    KeyAscii = 0
    Beep
    End Sub
     
    TJNJR, Feb 10, 2004
    #8
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.