#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 28
Default keypress

I am a bit confused about keypress.

I can't quite get this to work, there seems to be a few ways to do this.

What I want to do is

Allow a user to press any number (0123456789) in a text box.

I want to allow them to press backspace as well in that text box.

I want to shut off all other keys. When they have entered a 3 digit
number the focus will move to textbox 2 where I will do the same thing
there.

So, if I am on the right lines...

Private Sub TextBox1_KeyPress(KeyAscii As Integer)
tstinput (keyascii)
End Sub

private sub tstinput
If KeyAscii < 8 'allow backspace
Then
If Not IsNumeric(Chr$(KeyAscii)) Then
KeyAscii = 0
else
end if
else
end if
end sub

But this does not work, I am missing something here.

And when it is working I can add the length bit and move focus to next
textbox, I am content that I can do that at least. But I can not get the
hang of this keypress.

Any help appreciated.

Garry Jones
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 77
Default keypress


Hi

You could use something like this.


' in the general area

Const zero = 48
Const nine = 57
Const backspace = 8


'in the textbox keypress event

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If (KeyAscii < backspace) And (KeyAscii < zero Or KeyAscii nine) Then
KeyAscii = 0


End Sub

HTH

Ken


"Garry Jones" wrote in message
...
I am a bit confused about keypress.

I can't quite get this to work, there seems to be a few ways to do this.

What I want to do is

Allow a user to press any number (0123456789) in a text box.

I want to allow them to press backspace as well in that text box.

I want to shut off all other keys. When they have entered a 3 digit
number the focus will move to textbox 2 where I will do the same thing
there.

So, if I am on the right lines...

Private Sub TextBox1_KeyPress(KeyAscii As Integer)
tstinput (keyascii)
End Sub

private sub tstinput
If KeyAscii < 8 'allow backspace
Then
If Not IsNumeric(Chr$(KeyAscii)) Then
KeyAscii = 0
else
end if
else
end if
end sub

But this does not work, I am missing something here.

And when it is working I can add the length bit and move focus to next
textbox, I am content that I can do that at least. But I can not get the
hang of this keypress.

Any help appreciated.

Garry Jones




---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 09/10/2003


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,327
Default keypress

Hi Garry

You should also allow arrows and Delete, they're pretty standard. Try this:

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 8 To 10, 13, 27, 44 'Control characters
Case 48 To 57 'numbers
Case Else 'Discard anything else
KeyAscii = 0
End Select
End Sub

Big question is wether you allow pasting with Ctrl V into it. That takes
some effort to do, but it's done like like this:

Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 86 And Shift = 2 Then
KeyCode = 0 'cancelled !
'optional validation and pasting of
'clipboard content goes here
End If
End Sub


As for "next after 3 digits":

Private Sub TextBox1_KeyUp(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
If Len(TextBox1.Text) = 3 Then TextBox2.SetFocus
End Sub

--
HTH. Best wishes Harald
Followup to newsgroup only please

"Garry Jones" skrev i melding
...
I am a bit confused about keypress.

I can't quite get this to work, there seems to be a few ways to do this.

What I want to do is

Allow a user to press any number (0123456789) in a text box.

I want to allow them to press backspace as well in that text box.

I want to shut off all other keys. When they have entered a 3 digit
number the focus will move to textbox 2 where I will do the same thing
there.

So, if I am on the right lines...

Private Sub TextBox1_KeyPress(KeyAscii As Integer)
tstinput (keyascii)
End Sub

private sub tstinput
If KeyAscii < 8 'allow backspace
Then
If Not IsNumeric(Chr$(KeyAscii)) Then
KeyAscii = 0
else
end if
else
end if
end sub

But this does not work, I am missing something here.

And when it is working I can add the length bit and move focus to next
textbox, I am content that I can do that at least. But I can not get the
hang of this keypress.

Any help appreciated.

Garry Jones



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
Which cell was the activecell before a keypress excelent Excel Worksheet Functions 2 May 26th 06 01:45 PM
KeyPress Event Conrado Capistrano Excel Programming 4 September 25th 03 05:27 AM


All times are GMT +1. The time now is 01:32 PM.

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

About Us

"It's about Microsoft Excel"