469,927 Members | 1,978 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

textbox in vb accepting characters and numbers only

103 100+
Hi all
Wonder if anyone can help me out to solve this:

please find the code of textbox that accepts only characters and
one textbox that accepts only numbers:

textbox for accepting characters:
If txtname.Text = "" Then
MsgBox "enter the name", vbOKOnly, "PAYROLL"
Exit Sub
ElseIf IsNumeric(txtname.Text) Then
MsgBox "enter name",vbOKOnly, "PAYROLL"
With txtname
.SelStart = 0
.SelLength = Len(txtname.Text)
End With
End If

wat change i need for above code:?
if i press ny numbers instead that should not be displayed inside the

textbox for accepting numbers

Private Sub txtid_KeyPress(KeyAscii As Integer)
If (KeyAscii > 47 And KeyAscii < 58) Then
KeyAscii = KeyAscii
KeyAscii = 0
End If
End Sub

WAt change i need for above code:"
backspace is not working
and also i dont need a


please go thru this...and give a solutiion

Thanks in advance
Dec 19 '07 #1
2 7680
8,127 Expert 4TB
You need to handle key ascii in the proper range and handle ascii=8 for backspace.
Dec 19 '07 #2
866 512MB

textbox for accepting numbers

Expand|Select|Wrap|Line Numbers
  1. Private Sub txtid_KeyPress(KeyAscii As Integer)
  2. If (KeyAscii > 47 And KeyAscii < 58) Then 
  3. KeyAscii = KeyAscii
  4. Else
  5. KeyAscii = 0
  6. End If
  7. End Sub

Change your code with this one:

Expand|Select|Wrap|Line Numbers
  1. If KeyAscii = 8 Or KeyAscii = vbKeyDecimal Then
  2.         KeyAscii = 8
  3. Else
  4.         If KeyAscii < 48 Or KeyAscii > 57 Then
  5.             KeyAscii = 0
  6.         End If
  7. End If
Rey Sean
Dec 20 '07 #3

Post your reply

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

Similar topics

3 posts views Thread by Bisley | last post: by
2 posts views Thread by mg | last post: by
2 posts views Thread by ae | last post: by
2 posts views Thread by the friendly display name | last post: by
2 posts views Thread by Rain | last post: by
5 posts views Thread by ameen.abdullah | last post: by
3 posts views Thread by Michel Vanderbeke | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.