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

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

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

vbBack is string value

try Asc(vbBack)

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

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

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

Only a pleasure. Happy coding...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.