Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you capture PgDn or PgUp? 3

Status
Not open for further replies.

SiberBob

Programmer
Aug 28, 2002
107
US
I have checked the Autokeys and PgDn / PgUp are not available options to capture under AutoKeys. Is there another way to capture it? When I run the mdb file with the DB Window hidden the PgUp / PgDn keys don't do what I want. If I start the mdb with the Shift Key held so the DB window is visible, then run my form - the PgUp / PgDn move to previous / next records as I want. Any ideas?

Thanks
 
Each form has a KeyPress event that can be used to capture keystrokes, including special keys such as PgUp/PgDn/Home/End.
However, you need to set the "KeyPreview" property of the form to true because otherwise the active control receives the keypress before the form does, and doing it the first way means you need to code it only once per form rather than once per control.
Something like:

Code:
Private Sub Form_KeyPress (KeyAscii As Integer)

 If KeyAscii = vbKeyPageUp Or KeyAscii = vbKeyPageDown Then
       KeyAscii = 0
 End If
End Sub

All it does is nullify the keypress of pageup/pagedown by setting them to 0; all others are unaffected. This will only affect the form it is coded in, and will only work if you set the KeyPreview property of the form to True.

John
 
Ok,

I tried this: set the form's KeyPreview property to "yes" and added this sub

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)

 If KeyAscii = vbKeyPageUp Then MsgBox "Page Up", vbOKOnly, "Key"
 If KeyAscii = vbKeyPageDown Then MsgBox "Page Down", vbOnly, "Key"

End Sub

I put a breakpoint on the first IF line and it won't break when I press either pgup or pgdn. Eventually if I can get this to capture those keys I want to change the msgbox portion of the code to CmdPreviousRecord_Click and CmdNextRecord_Click to point to the Click Event code for those command buttons I have on the form.

Any ideas?

 
Hi!

Try the forms keydown event in stead:

[tt]Private Sub Form_KeyDown(KeyCode As Integer, Shift as integer)
If KeyCode = vbKeyPageUp Or KeyCode = vbKeyPageDown Then
KeyCode = 0
End If
End Sub[/tt]

Roy-Vidar
 
I had actually tried to code both events as follows and it still didn't work. I didn't include the KeyDown because I thought it my be extraneous information:

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)

 If KeyAscii = vbKeyPageUp Then MsgBox "Page Up", vbOKOnly, "Key"
 If KeyAscii = vbKeyPageDown Then MsgBox "Page Down", vbOnly, "Key"

End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
 
 If KeyAscii = vbKeyPageUp Then MsgBox "Page Up", vbOKOnly, "Key"
 If KeyAscii = vbKeyPageDown Then MsgBox "Page Down", vbOnly, "Key"


End Sub
[\code]
 
Test for the KeyCode not the KeyAsciiin the KeyDown event, as in my example.

Roy-Vidar
 
RoyVidar's code worked wonderful!

I had tried it (with KeyAscii) the first time you posted it and couldn't get it to work - they KeyPreview must be set to Yes.

Then I tried it again but messed up the KeyAscii part.

With KeyPreview and using RoyVidar's code above modified to

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
 
 If KeyCode = vbKeyPageUp Then CmdPreviousRecord_Click
 If KeyCode = vbKeyPageDown Then CmdNextRecord_Click


End Sub

Thanks!
 
RoyVidar's code worked wonderful!

I had tried it (with KeyAscii) the first time you posted it and couldn't get it to work - they KeyPreview must be set to Yes.

Then I tried it again but messed up the KeyAscii part.

It worked fine after I set KeyPreview = Yes and using RoyVidar's code above (modified to meet my needs):

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
 
 If KeyCode = vbKeyPageUp Then CmdPreviousRecord_Click
 If KeyCode = vbKeyPageDown Then CmdNextRecord_Click


End Sub

Thanks!
 
SiberBob, just a tip.
In the Declaration section of all your modules put this instruction:
Option Explicit
And in menu Tools -> Options ... -> Tab Editor be sure to have all the checkboxex ticked.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top