Keycodes with a form (1 Viewer)

keiths

Registered User.
Local time
Yesterday, 22:43
Joined
Feb 10, 2005
Messages
28
I am trying to trap specific key downs for use on a specific form.

Basically, what I want to have happen is when the specified form is open, I want to have the number keys mapped so that when I press the 9 key, it puts the number 9 in the field and then moves to the next field. Same with the other numbers on the keyboard...
So I used the following code:

----in on form load-----------
Me.KeyPreview = True

For the form's keydown property, I created the default private sub

Private Sub form_KeyDown(KeyCode As Integer, Shift As Integer)
'When number key is pressed, enter number plus tab to next field
Select Case KeyCode
Case vbKey9
SendKeys "9~"
Case vbKey8
SendKeys "8~"
Case vbKey7
SendKeys "7~"
End Select
End Sub

Now this works....except it goes into an "extreme" loop, and puts numbers in all the fields continuously until I do a Ctrl-Break.
Maybe I am all wet on using SendKeys....
Hope you guys can help.
 

WayneRyan

AWF VIP
Local time
Today, 05:43
Joined
Nov 19, 2002
Messages
7,122
Keith,

When you use SendKeys, it's just like you typed in the character from the
keyboard. Therefore for every keystroke, the KeyDown event calls
SendKeys, which calls the KeyDown event again, which calls SendKeys ...

You could try something like the following in your KeyDown event:

Code:
Dim i As Long
For i = 0 To Me.Controls.Count - 1
   If Me.Controls(i).Name = Me.ActiveControl.Name Then
      Me.Controls(i + 1).SetFocus
   End If
   Next i

That would just let the keystroke stand and move to the next sequential
control. This doesn't take into account "little things" like labels, or the
last control, but it might be a start.

Wayne
 

Users who are viewing this thread

Top Bottom