943,744 Members | Top Members by Rank

Ad:
Nov 19th, 2007
0

how to use the keypress function is visual basic 6

Expand Post »
hey need some help guys. for example i have a text box and i will have input in it. but only numeric ones are allowed to be inputted in the text box if a character for example "a" is pressed the text box will not change and type the "a" and if a numeric button for example is 1, the text box will have 1. how can i do this? thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dc_24l is offline Offline
23 posts
since Nov 2007
Jan 5th, 2008
0

Re: how to use the keypress function is visual basic 6

hi dc_241

try this
if keyascii>65 or keyascii<97
msgbox"error"
endif
Reputation Points: 10
Solved Threads: 1
Newbie Poster
vivekPSG is offline Offline
10 posts
since Dec 2007
Jan 5th, 2008
0

Re: how to use the keypress function is visual basic 6

try this following code :

' This code for string input or other (comma,drop,question mark etc) except numeric
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text1_KeyPress(KeyAscii As Integer)
  2. If KeyAscii < 48 Or KeyAscii > 57 Then
  3. Text1.Locked = False
  4. Else
  5. Text1.Locked = True
  6. End If
  7. End Sub

' This code for numeric input only, other input cannot be written
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text2_KeyPress(KeyAscii As Integer)
  2. If KeyAscii < 48 Or KeyAscii > 57 Then
  3. Text2.Locked = True
  4. Else
  5. Text2.Locked = False
  6. End If
  7. End Sub

' This code for string input only (drop, comma, question mark cannot be written)
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Text3_KeyPress(KeyAscii As Integer)
  2. If KeyAscii > 65 Or KeyAscii > 97 Then
  3. Text1.Locked = False
  4. Else
  5. Text1.Locked = True
  6. End If
  7. End Sub

actually you can combine this with ascii code. look the code in ascii table and you can let what input can be written.

OK.hope this helps..
Last edited by Jx_Man; Jan 5th, 2008 at 8:02 am. Reason: code tags
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Jan 5th, 2008
1

Re: how to use the keypress function is visual basic 6

You don't have to lock the text box, just set KeyAscii to 0. Also, you need to handle the backspace key to allow users to fix errors:

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57, 8
'Let these key codes pass through
Case Else
'All others get trapped
KeyAscii = 0
End Select
End Sub
Reputation Points: 40
Solved Threads: 24
Junior Poster
SCBWV is offline Offline
125 posts
since Apr 2007
Jan 5th, 2008
0

Re: how to use the keypress function is visual basic 6

thx SCBWV...
nice recovery for my post...
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Jan 7th, 2008
0

Re: how to use the keypress function is visual basic 6

Try this code if it help...

If (keyascii<48 or keyascii>57) and keyascii<>vbkeyback then
keyascii=0
end if
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Neophyte08 is offline Offline
7 posts
since Jan 2008
Mar 29th, 2009
0

Re: how to use the keypress function is visual basic 6

You can also try the isnumeric() function at the keypress event...
For example...

Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0
End Sub
Last edited by KSS; Mar 29th, 2009 at 5:38 pm.
KSS
Reputation Points: 11
Solved Threads: 3
Newbie Poster
KSS is offline Offline
21 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: how to print msflexgird and the data in this flexgrid
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Apply code to 'x' button on form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC