Simple way

in the form properties.. set KeyPreview = True

VB Code:
  1. Private Sub Form_KeyPress(KeyAscii As Integer)
  2.     If KeyAscii = 27 Then Unload Me
  3. End Sub