Results 1 to 8 of 8

Thread: number only problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2013
    Posts
    1,126

    number only problem

    To restrict my textbox intended for integer, i use keypress and coded it to accept only numbers but i realized i cant input period for decimal. How should this be properly done.

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: number only problem

    This problem has been discussed here and elsewhere numerous times already. Have you tried searching, codesearcher?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: number only problem

    Code:
    Option Explicit
    Const VALID_CHARS As String = "0123456789."
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
      If KeyAscii > 26 Then ' if it's not a control code
        If InStr(VALID_CHARS, Chr(KeyAscii)) = 0 Then
          KeyAscii = 0
        End If
      End If
    End Sub

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

    Re: number only problem

    Quote Originally Posted by Bonnie West View Post
    This problem has been discussed here and elsewhere numerous times already. Have you tried searching, codesearcher?
    Yepp, i even remember a thread where i posted a solution which only accepts one decimal separator
    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

  5. #5
    Hyperactive Member Lenggries's Avatar
    Join Date
    Sep 2009
    Posts
    353

    Re: number only problem

    Alternatively, you can validate the string after the input:

    Code:
    Private Sub Text1_Change()
        Dim strVal As String
        
        'use static local variable to manage reverting back to previous value
        Static strLast As String
    
        'trim the value
        strVal = Trim$(Text1.Text)
        
        If Len(Text1.Text) = 0 Then      
            'text field is blank. This is OK, but clearly no need to validate
            'NOTE: delete this first branch of the if statement if an empty next box is also invalid 
            '         (just make sure strLast get's seeded somehow with a valid piece of data... 
            '         might need ot use a modul level field insteadf of the static local)
            strLast = ""
        ElseIf Len(Text1.Text) <> Len(strVal) Then
            'something was trimmed. Set the field to the stripped value,
            'which will trigger this event handler again
            Text1.Text = strVal
        ElseIf IsNumeric(strVal) Then     
            'it's a number
            strLast = strVal
            'NOTE: You can replace IsNumeric() with a general call to a validate() 
            '         function if you want to restrict the type of values they can enter even further.
        Else
            'text field not numeric or blank. Revert to previous value
            'this will re-trigger the event handler, which is fine
            Text1.Text = strLast
        End If
    End Sub
    I personally prefer this framework when I validate textboxes because it gives me lot's of flexibility, plus it handles things well when someone pastes in a value (which doesn't always generate a keypress). The downside is that you can sometimes see a bit of flickering in the textbox when someone types in invalid characters... or when my cat walks across my keyboard.
    Last edited by Lenggries; Jul 26th, 2013 at 12:03 PM. Reason: fixed typos in code example

  6. #6

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

    Re: number only problem

    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
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: number only problem

    Quote Originally Posted by codesearcher View Post
    To restrict my textbox intended for integer, i use keypress and coded it to accept only numbers but i realized i cant input period for decimal. How should this be properly done.
    If it's intended that the TextBox holds only Integers why would you want to allow a decimal separator ?

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