Results 1 to 10 of 10

Thread: Insert only numbers in textbox

  1. #1

    Thread Starter
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287

    Insert only numbers in textbox

    Hello,
    How can I Force the user to insert only numbers in a textbox?
    Or in a different way, How can I check under the KeyPress event if the user pressed 0 to 9? (I think that would be better)

    Thanks
    Last edited by Stiletto; Mar 27th, 2007 at 08:20 PM.

  2. #2
    New Member
    Join Date
    Mar 2007
    Posts
    7

    Re: Insert only numbers in textbox

    Select the Text field in which you want to limit the user to numbers only. In it's properties, look for Dataformat, and click the "..." You should be able to choose General, Currancy, Number, Date, ect....

  3. #3
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Insert only numbers in textbox

    vb Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.   Select Case KeyAscii
    3.   Case vbKeyBack, vbKey0 To vbKey9
    4.     ' These keys are allowed to pass
    5.   Case Else
    6.     ' Everything else is blocked
    7.     KeyAscii = 0
    8.   End Select
    9. End Sub

  4. #4
    Member
    Join Date
    Mar 2007
    Posts
    44

    Re: Insert only numbers in textbox

    I think you could do it like this:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case Asc("0") To Asc("9"), vbKeyBack
            Case Else
                KeyAscii = 0
        End Select
    End Sub

  5. #5
    Member
    Join Date
    Mar 2007
    Posts
    44

    Re: Insert only numbers in textbox

    when I submited my reply, I found Logophobic had replied.
    I think vbKey0 would be better than Asc("0"), ha ha.

  6. #6

    Thread Starter
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287

    Re: Insert only numbers in textbox

    I'm starting to remember slowly how to do stuff after a long time i didnt touch VB. I got this
    Code:
    If KeyAscii < 48 Or KeyAscii > 57 Then
        If KeyAscii <> 8 Then
            MsgBox "Error"
            KeyAscii = 0
        End If
    End If
    Yours are better, I think I will go on the DataFormat suggestion. Thanks anyway

  7. #7

    Thread Starter
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287

    Re: Insert only numbers in textbox

    Quote Originally Posted by tanchuhan
    when I submited my reply, I found Logophobic had replied.
    I think vbKey0 would be better than Asc("0"), ha ha.
    Logophobic ?

  8. #8

    Thread Starter
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287

    Re: Insert only numbers in textbox

    Quote Originally Posted by Ghostpk
    Select the Text field in which you want to limit the user to numbers only. In it's properties, look for Dataformat, and click the "..." You should be able to choose General, Currancy, Number, Date, ect....
    Ive tried it, but it doesnt prevent me from typing letters...

  9. #9

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Insert only numbers in textbox

    How about
    Code:
    Private Sub Text1_Change()
    If Not IsNumeric(Text1.Text) Then
       Msgbox "Only numbers allowed."
       Text1.Text = vbNullString
    End If
    By putting it in the Change event you cover not only typing but also pasting.

Posting Permissions

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



Click Here to Expand Forum to Full Width