Disabling Letters

using keyASCII

Page 1 of 1

9 Replies - 2875 Views - Last Post: 23 March 2009 - 10:03 AM Rate Topic: -----

#1 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Disabling Letters

Posted 18 March 2009 - 11:51 PM

hi,
i'm still fixing my calculator and i've come across a problem. with my calculator, i want to disable any text and only allow numbers and ONLY one decimal to be typed in. this code is only for one radio button. i want it to work for all three that i have. can anyone help please?

Private Sub txtInvest_KeyPress(KeyAscii As Integer)
	If KeyAscii < 32 Then Exit Sub
		If Radio1.Value = True Then
				 If InStr("0123456789.", Chr$(KeyAscii)) = 0 Then
				KeyAscii = 0
		End If
	
	End If
End Sub
Private Sub txtRate_KeyPress(KeyAscii As Integer)
	If KeyAscii < 32 Then Exit Sub
		If Radio1.Value = True Then
			If InStr("0123456789.", Chr$(KeyAscii)) = 0 Then
				KeyAscii = 0
			End If
	End If
End Sub
Private Sub txtTerm_KeyPress(KeyAscii As Integer)
	If KeyAscii < 32 Then Exit Sub
		If Radio1.Value = True Then
			If InStr("0123456789.", Chr$(KeyAscii)) = 0 Then
				KeyAscii = 0
			End If
	End If
End Sub


This post has been edited by firebolt94: 19 March 2009 - 02:05 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Disabling Letters

#2 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 49
  • View blog
  • Posts: 2,489
  • Joined: 09-January 09

Re: Disabling Letters

Posted 19 March 2009 - 01:52 PM

check for the ascii code of letter pressed in the Keypress event of the txtbox which you are using

ASCII CODE OF CAPITAL A-Z------65-91
ASCII CODE OF SMALL a-z-------97-123

reject the letters with above ascii codes!

and about the decimal part, use a Instr function to check the presence of a "." in the text! :^:

if you want me to be more specific, WANT(reply)!

now, Say thanks! :P
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Disabling Letters

Posted 21 March 2009 - 09:39 AM

You don't even need to remember the ASCII Values of the characters.
Use the Asc() function like this:Asc("A") in your IF constructs to make your code more readable.
Was This Post Helpful? 0
  • +
  • -

#4 Raju2047  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 55
  • Joined: 16-February 09

Re: Disabling Letters

Posted 21 March 2009 - 10:54 AM

Hope this helps .
Public Function isNumber(ByVal sKeyAscii, ByRef txt As TextBox) As Integer
	If Not ((sKeyAscii >= 48 And sKeyAscii <= 57) Or sKeyAscii = 8 Or sKeyAscii = 46) Then
	isNumber = 0
	ElseIf sKeyAscii = 46 And InStr(txt.Text, ".") <> 0 Then
	isNumber = 0
	Else
	isNumber = sKeyAscii
	End If
End Function



call this function is each textbox_keypress event.

ex:
Private Sub txtTerm_KeyPress(KeyAscii As Integer)
keyascii=isnumber(keyascii,txtTerm)
End Sub



Thanx
Raju2047
Was This Post Helpful? 1
  • +
  • -

#5 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Disabling Letters

Posted 21 March 2009 - 05:25 PM

this doesnt answer my question as i wanted the function for my radio buttons

This post has been edited by firebolt94: 22 March 2009 - 01:36 AM

Was This Post Helpful? 0
  • +
  • -

#6 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Disabling Letters

Posted 22 March 2009 - 02:11 AM

No sorry, it actually does help.

thanks everyone
Was This Post Helpful? 0
  • +
  • -

#7 dioliew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 15-March 09

Re: Disabling Letters

Posted 22 March 2009 - 04:20 AM

Just think of a simple one that may suit your need.
Private Sub Text1_Change()
Dim AA As String
Dim BB
Dim EE
Dim intCycle As Long

EE = InStr(1, Text1, ".")
For intCycle = 1 To Len(Text1)
	AA = Mid(Text1, intCycle, 1)
	BB = AA Like "[0123456789.]"
	If BB = False Then
		Text1 = Mid(Text1, 1, intCycle - 1)
		Text1.SelStart = intCycle - 1
		Exit For
	End If
	If EE <> 0 And Len(Text1) > EE + 1 Then
		Text1 = Mid(Text1, 1, EE + 1)
		Text1.SelStart = EE + 1
		Exit For
	End If
	
Next
End Sub


Was This Post Helpful? 0
  • +
  • -

#8 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 49
  • View blog
  • Posts: 2,489
  • Joined: 09-January 09

Re: Disabling Letters

Posted 22 March 2009 - 10:12 PM

View Postborn2c0de, on 21 Mar, 2009 - 09:09 PM, said:

You don't even need to remember the ASCII Values of the characters.
Use the Asc() function like this:Asc("A") in your IF constructs to make your code more readable.


Ya thats one way, but its good if you remember ascii vals of atleast 1-9,A-Z,a-z.
Was This Post Helpful? 0
  • +
  • -

#9 firebolt  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 92
  • View blog
  • Posts: 5,561
  • Joined: 20-February 09

Re: Disabling Letters

Posted 22 March 2009 - 11:03 PM

Well thanks everyone but now the issue is in the Overflow error(another topic). But thanks anyways
Was This Post Helpful? 0
  • +
  • -

#10 shaneg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-February 09

Re: Disabling Letters

Posted 23 March 2009 - 10:03 AM

i got one you can use just place it in the module then behind the button just call it like this

this is for the module

 Public Sub CheckInteger(ByRef K As Integer)

 'routine that only allows numbers 0 to 9 and the backspace key
 'anything else gets ignored and the textbox gets resets to null value
 If (K < vbKey0 Or K > vbKey9) _
	And (K <> vbKeyBack) Then
		K = 0
  MsgBox "Please Enter Numbers Only!" & vbNewLine & "This TextBox is for Numbers Only!", vbInformation, "Numeric Only"
 End If
End Sub 


this is how you call it

 'Calls a routine to check if its a number between 0 and 9 also backspace if not resets text box
Private Sub txtQuantity1_KeyPress(KeyAscii As Integer)
Call CheckInteger(KeyAscii)
End Sub 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1