By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

getting only numbers by textbox.

P: 21
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.
Mar 21 '07 #1
Share this Question
Share on Google+
6 Replies


100+
P: 123
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
Mar 21 '07 #2

100+
P: 149
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.mindfiresolutions.com
Mar 21 '07 #3

vijaydiwakar
100+
P: 579
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. if instr(validstr,chr(skeyascii))=0 then 
  3. 'code to supress the keyascii
  4. else
  5. 'return same keyascii
  6. end if
  7.  
See this is the help line only now its ur duty to implement it further
Good Luck
Mar 21 '07 #4

100+
P: 375
No
here i'm giving u the best solution so far
Expand|Select|Wrap|Line Numbers
  1.  
  2. if instr(validstr,chr(skeyascii))=0 then 
  3. 'code to supress the keyascii
  4. else
  5. 'return same keyascii
  6. end if
  7.  
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(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
Mar 21 '07 #5

devonknows
100+
P: 137
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtR_KeyPress(KeyAscii As Integer)
  2. If  (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii = 8) Then
  3. 'keyascii =8 stands for backspace
  4. txtR.Locked = False
  5. Else
  6. txtR.Locked = True
  7. End If
  8. 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

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtR_KeyPress(KeyAscii As Integer)
  2.     If Not ( _
  3.         (KeyAscii >= "48" And KeyAscii <= "57") Or (KeyAscii = "8")) Then
  4.         KeyAscii = 0
  5.     End If
  6. End Sub
  7.  
Kind Regards
Devon.
Mar 21 '07 #6

100+
P: 375
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtR_KeyPress(KeyAscii As Integer)
  2. If  (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii = 8) Then
  3. 'keyascii =8 stands for backspace
  4. txtR.Locked = False
  5. Else
  6. txtR.Locked = True
  7. End If
  8. 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

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtR_KeyPress(KeyAscii As Integer)
  2.     If Not ( _
  3.         (KeyAscii >= "48" And KeyAscii <= "57") Or (KeyAscii = "8")) Then
  4.         KeyAscii = 0
  5.     End If
  6. End Sub
  7.  
Kind Regards
Devon.


Good suggestion Devon
Thanks
Mar 21 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.