From: mikearelli on
I think I understand what you're asking, and I'm looking for the same thing...

By setting the OnKeyDown event to [Event Procedure] the VBA code creates:
Private Sub TextBox1_OnKeyDown(KeyCode As Integer, Shift As Integer)

I want to use the same code for the OnKeyDown event of a number of textbox
controls on the same form, (TextBox1, TextBox2, TextBox3) but I only want the
code listed once. My thought was to update the originally built code from a
Sub to a Function:

Private Function KeyPressed(KeyCode As Integer, Shift As Integer)
Do something
End Function

Then in all of the controls OnKeyDown event, type in =KeyPressed(KeyCode,
Shift)
but you're right... Access changes it to =KeyPressed([KeyCode], [Shift]) and
doesn't understand what I'm trying to accomplish.

Can anyone suggest a method to allow me to use the same code for all textbox
OnKeyDown Events for the same form?

"Jon Lewis" wrote:

> Well unless I'm missing something I can't see how a Class will help as the
> scope of the KeyCode and Shift is local to each Key Down procedure so,
> unless you want to get into creating a keyboard hook which is a completely
> different ball game, the values have to be retrieved from within that
> procedure. Correct me if I'm wrong though...
>
> Jon
>
> "robeito" <robeito(a)discussions.microsoft.com> wrote in message
> news:ECA046AC-4B67-40B3-8592-60408AE6666E(a)microsoft.com...
> > Thanks Jon
> >
> > Your comment is clear to me, but I would like to write no extra code in
> > the
> > VBA window so maybe I sould try creating a class to handle the form's
> > events
> >
> >
>
>
> .
>
From: mikearelli on
Found a solution....

Set the OnKeyDown event for the FORM. not the individual controls.
ALSO set the FORM'S KeyPreview to On

Now, in the new sub that is created, I check to make sure the proper control
has the focus when the key is pressed. In this case, I'm looking for the
DELETE key to be pressed when I'm in any of my TextBox controls named
txtItem1, txtItem2, txtItem3.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 46 And Left(Me.ActiveControl.Name, 7) = "txtItem" Then
DoSomething
End If
End Sub

I hope this helps...

"mikearelli" wrote:

> I think I understand what you're asking, and I'm looking for the same thing...
>
> By setting the OnKeyDown event to [Event Procedure] the VBA code creates:
> Private Sub TextBox1_OnKeyDown(KeyCode As Integer, Shift As Integer)
>
> I want to use the same code for the OnKeyDown event of a number of textbox
> controls on the same form, (TextBox1, TextBox2, TextBox3) but I only want the
> code listed once. My thought was to update the originally built code from a
> Sub to a Function:
>
> Private Function KeyPressed(KeyCode As Integer, Shift As Integer)
> Do something
> End Function
>
> Then in all of the controls OnKeyDown event, type in =KeyPressed(KeyCode,
> Shift)
> but you're right... Access changes it to =KeyPressed([KeyCode], [Shift]) and
> doesn't understand what I'm trying to accomplish.
>
> Can anyone suggest a method to allow me to use the same code for all textbox
> OnKeyDown Events for the same form?
>
> "Jon Lewis" wrote:
>
> > Well unless I'm missing something I can't see how a Class will help as the
> > scope of the KeyCode and Shift is local to each Key Down procedure so,
> > unless you want to get into creating a keyboard hook which is a completely
> > different ball game, the values have to be retrieved from within that
> > procedure. Correct me if I'm wrong though...
> >
> > Jon
> >
> > "robeito" <robeito(a)discussions.microsoft.com> wrote in message
> > news:ECA046AC-4B67-40B3-8592-60408AE6666E(a)microsoft.com...
> > > Thanks Jon
> > >
> > > Your comment is clear to me, but I would like to write no extra code in
> > > the
> > > VBA window so maybe I sould try creating a class to handle the form's
> > > events
> > >
> > >
> >
> >
> > .
> >
First  |  Prev  | 
Pages: 1 2
Prev: Run Time Error 429
Next: Filter Form on Combo Variable