How do I code the 'On Key Press' VB function on a form?

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,462
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
I have an unbound form (it's like a 'help -> about' form) that I would like the user to be able to press the 'Esc' key to close the form rather than clicking a button. I have had a look through this forum, the MS site and Access help and I'm stuck - I'm trying to learn VB so please bear with me.

From what I gather I can set the 'key preview' event to 'yes' and then use the 'on key press' event to test for an ASCII character (I think it is ASCII number 27 for the 'Esc' key) and then close the form. So how do I code this in VB? Also, if I wanted to include other keys on the same function, such as the space bar and carriage return, how would these extra characters be added to the code? I'm using Access 2000.

Thanks in Advance
Andrew

Late Post : I realise I can do this with any key by using the following code, but would prefer to work off keys such as the 'Esc' key for when I get to other forms :
Private Sub Form_KeyPress(KeyAscii As Integer)
DoCmd.Close
End Sub
How would I add things like the 'Esc' and 'Space Bar' keys to this?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try this:

http://www.mvps.org/access/forms/frm0041.htm

On second glance, this could be improved slightly.
Instead of passing a String to the function that includes the function name or control name, pass a reference to the Control (ctr) as a Control instead.

Me.ActiveControl should do it. You can abbreviate the example to:

Code:
Call PlusMinus(KeyAscii, Me.ActiveControl)
Code:
Public Function PlusMinus(intKey As Integer, ctl As Control) As Integer
   
    ctl = CDate(ctl)
    
    Select Case intKey
        Case Is = 43        'the '+' key
            ctl = ctl + 1
            intKey = 0
        Case Is = 45        'the '-' keys
            ctl = ctl - 1
            intKey = 0
        Case Is = 61        'the '='/'+' key next to Backspace
            ctl = ctl + 1
            intKey = 0
    End Select
           
ExitHandler:
    PlusMinus = intKey
    Set ctl = Nothing
    Exit Function

TheHandler:
    Select Case Err.Number
        Case Is = 94    'Invalid use of null
        Case Is = 13    'Type mismatch
        Case Else
            MsgBox Err.Number & ":  " & Err.Description
            intKey = 0
    End Select
    Resume ExitHandler
End Function

Mike
 
Upvote 0
Andrew

Set the Cancel property of the command button to Yes.

Access Help said:
When a command button's Cancel property setting is Yes and the Form window is active, the user can choose the command button by pressing ESC, by pressing ENTER when the command button has the focus, or by clicking the command button.
[/code]
 
Upvote 0
Thanks for the suggestion Norie, it works exactly as suggested.
Thanks for the code Mike but I'm not sure how to modify it to test for the 'Esc' key and then to close the form.
Andrew. :)
 
Upvote 0
Resolved

Ok so I've worked it out and here is the code - in the end it was pretty simple - I just didn't know the syntax for the 'if' or the 'or' bits.
Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
  If KeyAscii = 27 Or KeyAscii = 32 Or KeyAscii = 13 Then
     DoCmd.Close
  End If
End Sub
Andrew
 
Upvote 0

Forum statistics

Threads
1,221,889
Messages
6,162,624
Members
451,778
Latest member
ragananthony7911

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top