onKeyDown: ¿how to call my own function?

R

robeito

Hello everybody:

Actual situation of my form:
onKeyDown= "[Event procedure]"

Desired situation:
onKeyDown= "myFunction"

I know that myFunction must be in a module, and must be public. But
I don't know the syntax for doing this !

I want to call myFunction from a lot of forms in my application, so, I want
to write it only once.
Also, I want to use the parameters KeyCode and Shift that uses the regular
call of the event.

Thanks in advance
 
J

Jon Lewis

You can normally type "=myFunction()" or if passing parameters
"=myFunction(par1, par2 etc)" (without the quotes) in the Property sheet.
However you want to pass the Key Down event's KeyCode and Shift values and
as far as I know you can only access them from within the Event Procedure so
you're going to have to use the procedure to at least get these values.

So have a public function saved in a Module written something like this:

Public Function myFunction (gKeyCode As Integer, gShift As Integer)
.......do stuff using gKeyCode & gShift
End Function

and then have the following in each form's On KeyDown event procedure:

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

On Key Down in the PropertySheet must of course be [Event procedure]

HTH
 
D

Daryl S

Robeito -

Leave the [Event procedure] there and click on the elipses to the right of
it (...). That will open up the code window for that event. In that code
window is where you will put your function call.

The function call will usually look something like this, depending on what
you need passed in and what you return from the function:

varReturnedValue = myFunction(passIn1, passIn2)

where passIn1 would be the first argument passed in to the function (if
there are any), etc.
 
R

robeito

Thanks Douglas
Your example works fine. Actually I'm trying something like this:

onKeyDown= "=myFunction(keycode,shift)"

but Access changes this to "=myFunction([keycode],[shift])"
and the calling is not working
 
R

robeito

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
 
J

Jon Lewis

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
 
D

Daryl S

Robeito -

Tell us what 'not working' means, and include your myFunction code, at least
the header. If you step through the code, what happens?
 
M

mikearelli

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?
 
M

mikearelli

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 said:
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 said:
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




.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top