Results 1 to 8 of 8

Thread: Numbers Only and - Only

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Numbers Only and - Only

    Hi All

    I am try to only type numbers in a textbox and stop letters from going into textbox which works fine, But I also want ------- to appear in textbox I looked up on the internet and should be vbKeySubtract so I added to one of the case lines but don't work just beeps

    Can someone please help me out with this thanks

    http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    'Accepts only numeric input

    Select Case KeyAscii

    Case vbKey0 To vbKey9

    Case vbKeyBack, vbKeyClear, vbKeyDelete, vbKeySubtract

    Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab

    Case Else

    KeyAscii = 0

    Beep

    End Select

    End Sub

    Kind Regards

    Steve

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Numbers Only and - Only

    The Ascii value for the minus sign is 45.

  3. #3
    Hyperactive Member
    Join Date
    May 2013
    Posts
    328

    Re: Numbers Only and - Only

    put it on keypress event

    Const key = "1234567890"

    If KeyAscii <> 0 Then

    If InStr(key, Chr(KeyAscii)) = 0 Then
    KeyAscii = 0
    End If

    End If

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: Numbers Only and - Only

    Per Markt....example
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
       Case vbKey0 To vbKey9
       
       Case 45
     
       Case vbKeyBack, vbKeyClear, vbKeyDelete       ', vbKeySubtract---remove
     
       Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
    
       Case Else
       KeyAscii = 0
       Beep
    End Select
    End Sub
    EDIT: FYI, vbKeySubtract is applicable in either KeyDown or KeyUp events but not
    on a KeyPress event.
    Last edited by SamOscarBrown; Jun 5th, 2013 at 07:26 AM.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: Numbers Only and - Only

    ..... and never forget, that in cases like this, a user still can copy&paste whatever characters he wishes into the Textbox......

    How about trapping the CTRL+V-Combination?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Numbers Only and - Only

    @zoni
    How do you trap ctrl v

    @Samoscarbrown

    I will give that a try later thanks

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8

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