Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default KeyPress Event

Hello NG,

Please tell me why the sendkeys statement does not work here in my code.
Also, why is it that when I press backspace it does not trigger the
keypress event even though the help file said that it will trigger the
event.

TIA

Jon-jon


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
SendKeys "{BACKSPACE}", True
Application.EnableEvents = True
MsgBox "Please enter numeric character only."
End Select
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default KeyPress Event

Conrado,

Try the following instead:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
MsgBox "Please enter numeric character only."
End Select
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com

"Conrado Capistrano" wrote in message
...
Hello NG,

Please tell me why the sendkeys statement does not work here in my code.
Also, why is it that when I press backspace it does not trigger the
keypress event even though the help file said that it will trigger the
event.

TIA

Jon-jon


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
SendKeys "{BACKSPACE}", True
Application.EnableEvents = True
MsgBox "Please enter numeric character only."
End Select
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default KeyPress Event

Just to add.
I have never seen a need to enable or disable events when you change the key
ascii value. I believe those two lines can be deleted.

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
KeyAscii = 0
MsgBox "Please enter numeric character only."
End Select
End Sub

worked fine for me without interfering with the users ability to edit the
information in the box. I saw no evidence that this event was fired by a
backspace key.

--
Regards,
Tom Ogilvy

"Chip Pearson" wrote in message
...
Conrado,

Try the following instead:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
MsgBox "Please enter numeric character only."
End Select
End Sub



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
www.cpearson.com

"Conrado Capistrano" wrote in message
...
Hello NG,

Please tell me why the sendkeys statement does not work here in my code.
Also, why is it that when I press backspace it does not trigger the
keypress event even though the help file said that it will trigger the
event.

TIA

Jon-jon


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
SendKeys "{BACKSPACE}", True
Application.EnableEvents = True
MsgBox "Please enter numeric character only."
End Select
End Sub






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default KeyPress Event

Try to avoid Sendkey Conrado
It is not reliable

Maybe you like this one
It will only allow digits 0 thru 9


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim i As Integer
If KeyAscii < 48 Or KeyAscii 57 Then
' only allow digits 0 thru 9
KeyAscii = 0
End If
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Conrado Capistrano" wrote in message ...
Hello NG,

Please tell me why the sendkeys statement does not work here in my code.
Also, why is it that when I press backspace it does not trigger the
keypress event even though the help file said that it will trigger the
event.

TIA

Jon-jon


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
SendKeys "{BACKSPACE}", True
Application.EnableEvents = True
MsgBox "Please enter numeric character only."
End Select
End Sub




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default KeyPress Event

Thanks Ron!


"Ron de Bruin" wrote in message
...
Try to avoid Sendkey Conrado
It is not reliable

Maybe you like this one
It will only allow digits 0 thru 9


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim i As Integer
If KeyAscii < 48 Or KeyAscii 57 Then
' only allow digits 0 thru 9
KeyAscii = 0
End If
End Sub


--
Regards Ron de Bruin
(Win XP Pro SP-1 XL2002 SP-2)
www.rondebruin.nl



"Conrado Capistrano" wrote in message

...
Hello NG,

Please tell me why the sendkeys statement does not work here in my code.
Also, why is it that when I press backspace it does not trigger the
keypress event even though the help file said that it will trigger the
event.

TIA

Jon-jon


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57 'Nos 0 - 9
Exit Sub
Case Else
Application.EnableEvents = False
SendKeys "{BACKSPACE}", True
Application.EnableEvents = True
MsgBox "Please enter numeric character only."
End Select
End Sub






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
event vba irosh Excel Worksheet Functions 4 November 11th 08 09:25 AM
VBA Event Steve Excel Discussion (Misc queries) 2 October 14th 08 11:04 PM
event marker68 Excel Discussion (Misc queries) 1 April 4th 08 02:38 PM
Which cell was the activecell before a keypress excelent Excel Worksheet Functions 2 May 26th 06 01:45 PM
Event Question bwilcox Excel Programming 1 July 21st 03 02:37 PM


All times are GMT +1. The time now is 10:26 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"