Results 1 to 10 of 10
  1. #1
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85

    Using the escape key to exit forms

    I am trying to set the escape key to close out of my form.


    here is the code I am using.
    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
    Select Case KeyCode
    Case vbKeyEsc
    KeyCode = 0
    End Select
     Select Case MsgBox("Are You Sure?", vbYesNo)
    Case vbYes
    If Me.Dirty Then
    DoCmd.RunCommand acCmdUndo
    End If
    DoCmd.Close acForm, "frmReceive", acSaveNo
    Case vbNo
    DoCmd.CancelEvent
    End Select
    End Sub
    As of now, when I press the esc key, the form is cleared and then the message box pops up.
    I want the message box to pop up before the form is cleared. Is this possible?

  2. #2
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    I just realized that any key that I press is making this code run.

  3. #3
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    I found this.
    http://www.techonthenet.com/access/f...isable_esc.php

    that link is about disabling the escape key, but we can pull this code and you can use it however you'd like.


    Let me know how it turns out.

    Code:
    Private Sub Form_KeyPress(KeyCode As Integer)
    
    
    If KeyCode = vbKeyEscape Then
       ClearConfirm = MsgBox("Are You Sure", vbYesno,"Quit?")
        if ClearConfirm = vbyes then
            If Me.Dirty Then
                DoCmd.RunCommand acCmdUndo
            End If
            DoCmd.Close acForm, "frmReceive", acSaveNo
        end if    
    else
        DoCmd.CancelEvent    
    End If
    End Sub
    Last edited by redbull; 05-31-2016 at 01:29 PM. Reason: John_G caught the KeyAscii/KeyCode conflict.

  4. #4
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    That code won't work as written - the Parameter is KeyAscii, but you are checking KeyCode.

    Joecamel -

    You could also use a command button on the form to do this (caption it "Quit" or something like that), and put the code below into its On Click event. Set the Cancel property of the command button to Yes. The result is that if you Press ESC while the form is open, that On Click event code will run. It is easier than using KeyPress and checking which character was pressed.

    KeyPress is great for intercepting and ignoring the Esc (or any other) key, though.

    Code:

    Code:
    Sub QuitButton_OnClick
    if  MsgBox("Are You Sure?", vbYesNo) = vbYes then
      If Me.Dirty Then
        DoCmd.RunCommand acCmdUndo
      End If
      DoCmd.Close acForm, "frmReceive", acSaveNo
    Endif
    
    End Sub

  5. #5
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    @Redbull
    That didn't work. Ive already seen the link that you put up.

    @ John G. Your suggestion kinda worked. It still clears the data from the form before the message box pops up.
    Users are accustom to using the escape key to exit the forms, which is why I want to use this along with the close button.
    The only thing is is that I don't want the form to clear out until after the selection from the message box.

  6. #6
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Quote Originally Posted by joecamel9166 View Post
    @Redbull
    That didn't work. Ive already seen the link that you put up.

    Any specific error? What line?

  7. #7
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    No error, but it had the same effect (as far as I could tell) as the code I had in place.

  8. #8
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I found the answer. You're using the wrong event - use the KeyDown event instead. The KeyPress clears the form and then does the msgbox, whereas the KeyDown asks first.

    Here is suggested code:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      If KeyCode <> 27 Then Exit Sub  ' Ignore everything except the Esc key
      if MsgBox("Are You Sure?", vbYesNo) = vbno then
        keycode = 0   ' Cancels the Esc key
        exit sub
      endif
      If Me.Dirty Then
        DoCmd.RunCommand acCmdUndo
      End If
      DoCmd.Close acForm, "frmReceive", acSaveNo
    
    End Sub

  9. #9
    redbull's Avatar
    redbull is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Mar 2012
    Location
    Missouri
    Posts
    480
    Quote Originally Posted by John_G View Post
    I found the answer. You're using the wrong event - use the KeyDown event instead. The KeyPress clears the form and then does the msgbox, whereas the KeyDown asks first.

    Man I wish I had access installed on my computer, good catch.

  10. #10
    joecamel9166 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2016
    Posts
    85
    John, that worked like a charm.
    Thanks Guys!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exit forms without saving
    By sk88 in forum Access
    Replies: 19
    Last Post: 12-19-2013, 06:04 PM
  2. Replies: 2
    Last Post: 12-20-2012, 03:06 PM
  3. Save Data so Escape key doesn't work
    By MintChipMadness in forum Forms
    Replies: 13
    Last Post: 08-16-2012, 01:24 PM
  4. Escape field entry
    By torpid in forum Programming
    Replies: 3
    Last Post: 06-05-2011, 08:30 PM
  5. Line Return escape character
    By Jerry8989 in forum Access
    Replies: 8
    Last Post: 10-02-2009, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums