+ Reply to Thread
Results 1 to 5 of 5

Textbox edit

Hybrid View

Guest Textbox edit 04-18-2005, 01:06 PM
Guest RE: Textbox edit 04-18-2005, 01:06 PM
Guest Re: Textbox edit 04-18-2005, 01:06 PM
davidm For numeric validation, how... 04-18-2005, 01:57 PM
Guest Re: Textbox edit 04-18-2005, 02:06 PM
  1. #1
    Paul
    Guest

    Textbox edit

    I have a text box in a form I would like the user to enter numbers only in.
    On this forum, some code was posted:

    Private Sub TextBox1_Key Press
    If KeyAscii <48 or Key Ascii >57 Then
    KeyAscii=0
    End If

    This works great EXCEPT it will not allow input of a negative number, which
    I need. How can I modify this code to allow a negative number to be input?

    Thanks!

    --
    Paul

  2. #2
    K Dales
    Guest

    RE: Textbox edit

    The ASCII code for "-" is 45, so you can allow that; i.e:
    If (KeyAscii < 48 and KeyAscii <>45) or KeyAscii > 57 Then...
    The only problem: the user could enter something like 34-56 unless you also
    check to see if it is the first character in the textbox, so you may need to
    write some code to deal with this possibility.

    "Paul" wrote:

    > I have a text box in a form I would like the user to enter numbers only in.
    > On this forum, some code was posted:
    >
    > Private Sub TextBox1_Key Press
    > If KeyAscii <48 or Key Ascii >57 Then
    > KeyAscii=0
    > End If
    >
    > This works great EXCEPT it will not allow input of a negative number, which
    > I need. How can I modify this code to allow a negative number to be input?
    >
    > Thanks!
    >
    > --
    > Paul


  3. #3
    Ron de Bruin
    Guest

    Re: Textbox edit

    Hi Paul

    Use this (45 = -)

    Private Sub TextBox1_KeyPress(ByVal keyascii As MSForms.ReturnInteger)
    Select Case keyascii
    Case 45
    Case 48 To 57 'Numbers
    Case Else 'Discard anything else
    keyascii = 0
    End Select
    End Sub


    --
    Regards Ron de Bruin
    http://www.rondebruin.nl



    "Paul" <Paul@discussions.microsoft.com> wrote in message news:6EE5BA91-4AED-4FDB-A99D-BC04A5D8A5E5@microsoft.com...
    >I have a text box in a form I would like the user to enter numbers only in.
    > On this forum, some code was posted:
    >
    > Private Sub TextBox1_Key Press
    > If KeyAscii <48 or Key Ascii >57 Then
    > KeyAscii=0
    > End If
    >
    > This works great EXCEPT it will not allow input of a negative number, which
    > I need. How can I modify this code to allow a negative number to be input?
    >
    > Thanks!
    >
    > --
    > Paul




  4. #4
    Forum Contributor
    Join Date
    03-03-2005
    Posts
    316
    For numeric validation, how about..

    Private sub TextBox1_Change()
    If Not Isnumeric(TextBox1.value) then
    MsgBox "Enter a numeric value"
    Exit Sub
    End if
    End sub

    DM

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1