1,105,331 Community Members

How to disable letters?

Member Avatar
masterfact18
Light Poster
43 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

im creating a simple calculator and i dont know how to disable letter in the textbox.. can someone help me please...newbie here....BTW im using VB6...

Member Avatar
rishif2
Posting Whiz in Training
284 posts since Dec 2012
Reputation Points: 57 [?]
Q&As Helped to Solve: 58 [?]
Skill Endorsements: 4 [?]
 
0
 

if the inputted character is letter then
use the statement

KeyAscii=0

you have to write that code on the key press event of the text box

now i hope that you can handle the issue with ASCII VALUES of letters

if you think letters as alphabets then ascii value range is 65 to 90 and 97 to 122

Member Avatar
masterfact18
Light Poster
43 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 
If (KeyAscii <= vbKey9 And KeyAscii >= vbKey0 Or KeyAscii = vbBack) Then
Else
KeyAscii = 0
End If

found this code on youtube but it doesnt on me i got a debug...
can you tell me whats wrong with that code...i put it in the keypress on the textbox...

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

vbBack is string value

try Asc(vbBack)

Member Avatar
rishif2
Posting Whiz in Training
284 posts since Dec 2012
Reputation Points: 57 [?]
Q&As Helped to Solve: 58 [?]
Skill Endorsements: 4 [?]
 
0
 

try this

If Not (KeyAscii <= vbKey9 And KeyAscii >= vbKey0 Or KeyAscii = vbKeyBack) Then
KeyAscii = 0
End If

hope this helps you to solve the issue

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

There are 2 ways to do this -

  1. When your textbox looses focus, trap the error...

    If Not IsNumeric(Text1.Text) Then

    MsgBox "Numeric input only!", vbCritical, "Invalid Input"

    Text1.Text = ""

    End If

OR, you can just delete the letter that was typed so it only accepts numbers...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Text1.Text) Then 
SendKeys {Backspace}
End If
End Sub

''Note, you HAVE to reference Microsoft Scripting Library in your project...
''This will delete any letters or symbols except for numeric values. The ".," is allowed...
Member Avatar
masterfact18
Light Poster
43 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Wow thanks for the help everyone i really learned a lot..
i tried all your answer and it really works... thanks again everyone!!!

Question Answered as of 1 Year Ago by rishif2, AndreRet and TnTinMN
Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

Only a pleasure. Happy coding...

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: