By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,455 Members | 2,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,455 IT Pros & Developers. It's quick & easy.

KeyPress Event

P: 7
After searching the forum, I found information on the KeyPress Event and am able to make it work by defining a private sub for each field that it applies to. But, I have to many fields to apply it to and want to create a public sub or function that is entered into the keypress event for each field.

The question is, how do I reference the key value in the function or sub that is entered into the event field? Something like =IncDec(KeyAscii) doesn't work.
Jan 4 '07 #1
Share this Question
Share on Google+
6 Replies


missinglinq
Expert 2.5K+
P: 3,532
What exactly is it you're trying to do with KeyPress i.e make the <Tab> key actually tab spaces on memo fields, make <Enter> goto new line in certain controls, etc? The more info we have the easier it is for us to help you! Always post actual code when appropriate!

And welcome to forum!
Jan 4 '07 #2

hariharanmca
100+
P: 1,977
After searching the forum, I found information on the KeyPress Event and am able to make it work by defining a private sub for each field that it applies to. But, I have to many fields to apply it to and want to create a public sub or function that is entered into the keypress event for each field.

The question is, how do I reference the key value in the function or sub that is entered into the event field? Something like =IncDec(KeyAscii) doesn't work.

whatever method you define with keyAscii as integer and shift as integer then call that method with and appropriate argument with keypress event
Jan 4 '07 #3

P: 7
The script will trap the + and - keys to increment/decrement the date. The code to do this, below, is not an issue. How to call it is. It works just fine as a private function for the control ( ). But since I have about 30 dates I don't want to have 30 copies of the same code. As a work around, I could create a private function for each control that just calls a single public routine that does the work. But I want to have just one global (public) routine that is called from the On KeyPress Event. How do I reference the key in the call? Neither =ControlName_Keypress(KeyAscii) or Call ControlName_Keypress(KeyAscii) seem to work.


Private Sub ControlName_KeyPress(KeyAscii As Integer)
elect Case KeyAscii
Case 43 'Plus Key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 'Minus Key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub
Jan 4 '07 #4

hariharanmca
100+
P: 1,977
The script will trap the + and - keys to increment/decrement the date. The code to do this, below, is not an issue. How to call it is. It works just fine as a private function for the control ( ). But since I have about 30 dates I don't want to have 30 copies of the same code. As a work around, I could create a private function for each control that just calls a single public routine that does the work. But I want to have just one global (public) routine that is called from the On KeyPress Event. How do I reference the key in the call? Neither =ControlName_Keypress(KeyAscii) or Call ControlName_Keypress(KeyAscii) seem to work.


Private Sub ControlName_KeyPress(KeyAscii As Integer)
elect Case KeyAscii
Case 43 'Plus Key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl + 1
Case 45 'Minus Key
KeyAscii = 0
Screen.ActiveControl = Screen.ActiveControl - 1
End Select
End Sub


Just use like this

if you know that you are useing same control then give controlName
like

public Sub ControlName1_KeyPress(pScreen As Screen,KeyAscii As Integer)
elect Case KeyAscii
Case 43 'Plus Key
KeyAscii = 0
pScreen.ActiveControl = pScreen.ActiveControl + 1
Case 45 'Minus Key
KeyAscii = 0
pScreen.ActiveControl = pScreen.ActiveControl - 1
End Select
End Sub



private Sub ControlName_KeyPress(KeyAscii As Integer)
ControlName1_KeyPress Screen, KeyAscii
End Sub
Jan 4 '07 #5

P: 7
Thanks for the response.

I have done what you suggested and it works, but I want to avoid creating a private function for each control that is function will be used for even if the function is just a shell. I want to call the public sub directly from the keypress event, but don't know the name of the argument that passes the key value.

Gary
Jan 4 '07 #6

MMcCarthy
Expert Mod 10K+
P: 14,534
Thanks for the response.

I have done what you suggested and it works, but I want to avoid creating a private function for each control that is function will be used for even if the function is just a shell. I want to call the public sub directly from the keypress event, but don't know the name of the argument that passes the key value.

Gary
Expanding on Hari's example

Change this to a function.

Expand|Select|Wrap|Line Numbers
  1. Function ControlKeyPress(pScreen As Screen,KeyAscii As Integer)
  2.  
  3.    Select Case KeyAscii
  4.    Case 43 'Plus Key
  5.       KeyAscii = 0
  6.       pScreen.ActiveControl = pScreen.ActiveControl + 1
  7.    Case 45 'Minus Key
  8.       KeyAscii = 0
  9.       pScreen.ActiveControl = pScreen.ActiveControl - 1
  10.    End Select
  11.  
  12. End Function
  13.  
You can call it like this ...

Expand|Select|Wrap|Line Numbers
  1. Private Sub ControlName_KeyPress(KeyAscii As Integer)
  2.    ControlKeyPress Screen, KeyAscii
  3. End Sub
  4.  
Mary
Jan 5 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.