+ Reply to Thread
Results 1 to 5 of 5

Textbox edit

  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

  5. #5
    Bob Phillips
    Guest

    Re: Textbox edit

    even nicer!

    A better solution, and highlighting that my fHyphen variable was superfluous
    (in such a nice way :-))

    Bob


    "Ron de Bruin" <rondebruin@kabelfoon.nl> wrote in message
    news:%23P$9OhDRFHA.1476@TK2MSFTNGP09.phx.gbl...
    > Hi Bob
    >
    >
    > Or maybe
    >
    > Case 45: If TextBox1.SelStart <> 0 Then keyascii = 0
    >
    >
    > --
    > Regards Ron de Bruin
    > http://www.rondebruin.nl
    >
    >
    >
    > "Bob Phillips" <bob.phillips@notheretiscali.co.uk> wrote in message

    news:%23PX2ieDRFHA.164@TK2MSFTNGP12.phx.gbl...
    > > Only once, and at start Ron
    > >
    > > Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    > > Static fHyphen As Boolean
    > > Select Case KeyAscii
    > > Case 45
    > > If fHyphen Or Len(TextBox1.Text) > 1 Then
    > > KeyAscii = 0
    > > End If
    > > Case 48 To 57 'Numbers
    > > Case Else 'Discard anything else
    > > KeyAscii = 0
    > > End Select
    > > End Sub
    > >
    > >
    > > Regards
    > >
    > > Bob
    > >
    > >
    > > "Ron de Bruin" <rondebruin@kabelfoon.nl> wrote in message
    > > news:OUVdWSDRFHA.204@TK2MSFTNGP15.phx.gbl...
    > >> 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
    > >>
    > >>

    > >
    > >

    >
    >




+ 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