Solved

Unload form just by pressing 'Escape' key

Posted on 2000-03-20
5
224 Views
Last Modified: 2012-05-05
Is there anyone who knows how to unload a form by merely pressing escape key?

I have tried keypress event of a form but the event is not triggered at all. And i would also like to know the ascii code of the escape key. Thank you.
0
Comment
Question by:klackons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 2635745
form1.keypreview = true


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

If KeyCode = 27 And Shift = 0 Then Unload Me

End Sub
0
 
LVL 18

Accepted Solution

by:
deighton earned 25 total points
ID: 2635749
form1.keypreview = true


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

                   If KeyCode = 27 And Shift = 0 Then Unload Me

                   End Sub


the escape key is ascii code 27
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 2635753
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 27) Then Unload Me
End Sub

don't forget to set keypreview property (on your form) to true
0
 
LVL 18

Expert Comment

by:deighton
ID: 2635774
vbKeyEscape

is the built in VB key word for the escape key

therefore

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

     If KeyCode = vbKeyEscape And Shift = 0 Then Unload Me

End Sub

Cheers!
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2635989
Alternatively:

Put a button on your form and set its Cancel property to true. Now, hitting the Esc key will execute the associated _click procedure.

Exit from there.

If the button must not be visible, set its left position to an appropriately large negative value and its TabStop property to false.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question