We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,509 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to disable letters?

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...

4
Contributors
7
Replies
1 Day
Discussion Span
2 Months Ago
Last Updated
29
Views
Question
Answered
masterfact18
Light Poster
43 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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

rishif2
Junior Poster
153 posts since Dec 2012
Reputation Points: 54
Solved Threads: 31
Skill Endorsements: 3
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...

masterfact18
Light Poster
43 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

vbBack is string value

try Asc(vbBack)

TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13

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

rishif2
Junior Poster
153 posts since Dec 2012
Reputation Points: 54
Solved Threads: 31
Skill Endorsements: 3

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...
AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

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

masterfact18
Light Poster
43 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 2 Months Ago by rishif2, AndreRet and TnTinMN

Only a pleasure. Happy coding...

AndreRet
Industrious Poster
4,706 posts since Jan 2008
Reputation Points: 391
Solved Threads: 481
Skill Endorsements: 20

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0794 seconds using 2.69MB