Solved

Unload form just by pressing 'Escape' key

Posted on 2000-03-20
5
202 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
  • 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now