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

textbox in vb accepting characters and numbers only

100+
P: 103
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:

(vb)
textbox for accepting characters:
If txtname.Text = "" Then
MsgBox "enter the name", vbOKOnly, "PAYROLL"
txtname.SetFocus
Exit Sub
ElseIf IsNumeric(txtname.Text) Then
MsgBox "enter name",vbOKOnly, "PAYROLL"
With txtname
.SelStart = 0
.SelLength = Len(txtname.Text)
.SetFocus
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

code:vb
textbox for accepting numbers

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

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

isnumeric(txtid.text)
code

please go thru this...and give a solutiion

Thanks in advance
Dec 19 '07 #1
Share this Question
Share on Google+
2 Replies


debasisdas
Expert 5K+
P: 8,127
You need to handle key ascii in the proper range and handle ascii=8 for backspace.
Dec 19 '07 #2

lotus18
100+
P: 866

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
Hi

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.