getting only numbers by textbox.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vandanasridhar
    New Member
    • Mar 2007
    • 21

    getting only numbers by textbox.

    hi, myself vandana. is there any property of textbox that can allow textbox to accept only numbers. i tried it by setting dataformatting property to number but nothing happens.
    thx in adv.
  • Shailja
    New Member
    • Feb 2007
    • 123

    #2
    Originally posted by vandanasridhar
    hi, myself vandana. is there any property of textbox that can allow textbox to accept only numbers. i tried it by setting dataformatting property to number but nothing happens.
    thx in adv.

    Use Isnumeric function. If character is enetered check by following code:
    If not Isnumeric(text1 .text) then
    MsgBox "Only numeric value is allowed"
    End if

    Comment

    • ansumansahu
      New Member
      • Mar 2007
      • 149

      #3
      Originally posted by vandanasridhar
      hi, myself vandana. is there any property of textbox that can allow textbox to accept only numbers. i tried it by setting dataformatting property to number but nothing happens.
      thx in adv.
      You can also write a separate sub / function that will trap the Ascii value of the key pressed and check it if its a number. Ascii value 48 To 57 allow numbers.

      thanks
      ansuman sahu
      www.mindfiresol utions.com

      Comment

      • vijaydiwakar
        Contributor
        • Feb 2007
        • 579

        #4
        Originally posted by vandanasridhar
        hi, myself vandana. is there any property of textbox that can allow textbox to accept only numbers. i tried it by setting dataformatting property to number but nothing happens.
        thx in adv.
        No
        here i'm giving u the best solution so far
        Code:
         
        if instr(validstr,chr(skeyascii))=0 then 
        'code to supress the keyascii
        else
        'return same keyascii
        end if
        See this is the help line only now its ur duty to implement it further
        Good Luck

        Comment

        • cmrhema
          Contributor
          • Jan 2007
          • 375

          #5
          Originally posted by vijaydiwakar
          No
          here i'm giving u the best solution so far
          Code:
           
          if instr(validstr,chr(skeyascii))=0 then 
          'code to supress the keyascii
          else
          'return same keyascii
          end if
          See this is the help line only now its ur duty to implement it further
          Good Luck
          How do anyone rate this code
          Private Sub txtR_KeyPress(K eyAscii As Integer)
          If (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii = 8) Then
          'keyascii =8 stands for backspace
          txtR.Locked = False
          Else
          txtR.Locked = True
          End If
          End Sub

          Comment

          • devonknows
            New Member
            • Nov 2006
            • 137

            #6
            Code:
            Private Sub txtR_KeyPress(KeyAscii As Integer)
            If  (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii = 8) Then
            'keyascii =8 stands for backspace
            txtR.Locked = False
            Else
            txtR.Locked = True
            End If
            End Sub
            Instead of locking it change the KeyAscii to 0 which classes as no key was pressed. The other thing i may suggest thinking about is adding something to the txtR_Change() sub so that they cant right click and paste in

            Code:
            Private Sub txtR_KeyPress(KeyAscii As Integer)
                If Not ( _
                    (KeyAscii >= "48" And KeyAscii <= "57") Or (KeyAscii = "8")) Then
                    KeyAscii = 0
                End If
            End Sub
            Kind Regards
            Devon.

            Comment

            • cmrhema
              Contributor
              • Jan 2007
              • 375

              #7
              Originally posted by devonknows
              Code:
              Private Sub txtR_KeyPress(KeyAscii As Integer)
              If  (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii = 8) Then
              'keyascii =8 stands for backspace
              txtR.Locked = False
              Else
              txtR.Locked = True
              End If
              End Sub
              Instead of locking it change the KeyAscii to 0 which classes as no key was pressed. The other thing i may suggest thinking about is adding something to the txtR_Change() sub so that they cant right click and paste in

              Code:
              Private Sub txtR_KeyPress(KeyAscii As Integer)
                  If Not ( _
                      (KeyAscii >= "48" And KeyAscii <= "57") Or (KeyAscii = "8")) Then
                      KeyAscii = 0
                  End If
              End Sub
              Kind Regards
              Devon.


              Good suggestion Devon
              Thanks

              Comment

              Working...