Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default TextBox and KeyCode

I've got a userform with multiple textboxes, each corresponding to
different types of data. I'm trying to limit the possible errors that
can occur. One of my textboxes, corresponds to a zip code. The code I
have to restrict possible entries to number is as so:

Private Sub ZipCode_KeyDown(ByVal KeyCode As _ MSForms.ReturnInteger,
ByVal Shift As Integer)

If KeyCode < 48 Or KeyCode 57 Then
KeyCode = 0
Beep
End If
End Sub

However, the two problems I have is that. A) The user cannot use the
keypad. What are the keycodes to enable the use of the keypad. B) The
user cannot use the backspace key, to correct an entry. How can I limit
the entries to numbers, but allow them to use the keypad? And allow them
to correct themselves, with the backspace key? Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default TextBox and KeyCode

Private Sub ZipCode_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
With Me.ZipCode
Select Case KeyCode
Case KeyCode < 48, Is 57
KeyCode = 0
Beep
Case 8 'backspace
If Len(.Text) 0 Then
.Text = Left(.Text, Len(.Text) - 1)
End If
KeyCode = 0
End Select
End With
End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Darrin Henshaw" wrote in message
...
I've got a userform with multiple textboxes, each corresponding to
different types of data. I'm trying to limit the possible errors that
can occur. One of my textboxes, corresponds to a zip code. The code I
have to restrict possible entries to number is as so:

Private Sub ZipCode_KeyDown(ByVal KeyCode As _ MSForms.ReturnInteger,
ByVal Shift As Integer)

If KeyCode < 48 Or KeyCode 57 Then
KeyCode = 0
Beep
End If
End Sub

However, the two problems I have is that. A) The user cannot use the
keypad. What are the keycodes to enable the use of the keypad. B) The
user cannot use the backspace key, to correct an entry. How can I limit
the entries to numbers, but allow them to use the keypad? And allow them
to correct themselves, with the backspace key? Thanks.

*** Sent via Developersdex http://www.developersdex.com ***



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default TextBox and KeyCode

Thanks Bob. That takes care of the need to backspace, but any ideas on
how to allow them to use the numeric keypad?

*** Sent via Developersdex http://www.developersdex.com ***
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default TextBox and KeyCode

Sorry about that

Private Sub ZipCode_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
With Me.ZipCode
Select Case True
Case (KeyCode = 96 And KeyCode < 105) 'numeric keypad
'exit quietly
Case (KeyCode = 48 And KeyCode < 57) 'normal keypad
'exit quietly
Case KeyCode = 8 'backspace
If Len(.Text) 0 Then
.Text = Left(.Text, Len(.Text) - 1)
End If
KeyCode = 0
Case Else
KeyCode = 0
Beep
End Select
End With
End Sub


--
HTH

Bob Phillips

"Darrin Henshaw" wrote in message
...
Thanks Bob. That takes care of the need to backspace, but any ideas on
how to allow them to use the numeric keypad?

*** Sent via Developersdex http://www.developersdex.com ***



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default TextBox and KeyCode

As long as the Num Lock is on the keypad should return the same ascii values
as the keyboard number keys.

"Darrin Henshaw" wrote:

Thanks Bob. That takes care of the need to backspace, but any ideas on
how to allow them to use the numeric keypad?

*** Sent via Developersdex http://www.developersdex.com ***



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default TextBox and KeyCode

Thanks it's working now. Is there a place I can see what the values are
for each key? So I can print them, or save them for future reference.
Also, by just modifying the "Me.ZipCode" can use the same code for
another textbox, as is? Thanks.


*** Sent via Developersdex http://www.developersdex.com ***
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default TextBox and KeyCode

There is a list of Ascii characters in VB help. Or you can set a debug
breakpoint and examine the value, or use Debug.Print KeyCode, or whatever.

"Darrin Henshaw" wrote:

Thanks it's working now. Is there a place I can see what the values are
for each key? So I can print them, or save them for future reference.
Also, by just modifying the "Me.ZipCode" can use the same code for
another textbox, as is? Thanks.


*** Sent via Developersdex http://www.developersdex.com ***

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default TextBox and KeyCode

Yes, we often modify existing code or create module that can be used in many
other places.

"Darrin Henshaw" wrote:

Thanks it's working now. Is there a place I can see what the values are
for each key? So I can print them, or save them for future reference.
Also, by just modifying the "Me.ZipCode" can use the same code for
another textbox, as is? Thanks.


*** Sent via Developersdex http://www.developersdex.com ***

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
Keycode description creator Beeatrice Excel Discussion (Misc queries) 1 November 12th 09 02:08 AM
HELP! I Lost The Ability To Advance From TextBox To TextBox With the ENTER Or The TAB Keys Minitman[_4_] Excel Programming 0 February 22nd 05 08:50 PM
detecting keycode internationally charlie Excel Programming 0 December 20th 04 09:42 PM
Textbox Bug? Missing/delayed update of textbox filled via VBA MarcM Excel Programming 0 November 4th 04 05:47 PM
Textbox Bug? Missing/delayed update of textbox filled via VBA MarcM Excel Programming 0 November 4th 04 05:43 PM


All times are GMT +1. The time now is 09:53 PM.

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"