trouble with escape key

Crash1hd

Registered CyberGeek
Local time
Today, 16:55
Joined
Jan 11, 2004
Messages
143
I created a action so that when I press the esc key it closes the form
Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
        DoCmd.Close
    End If
End Sub
however I have the following code in the form as well
Code:
Sub Form_Unload(Cancel As Integer)
On Error GoTo Form_Unload_Err
If Checkme = True Then
Dim strMsg As String
    strMsg = "Data has changed."
    strMsg = strMsg & " Do you wish to save the changes?" & Chr(13)
    strMsg = strMsg & "Click 'Yes' to Save or 'No' to Discard the changes."

Select Case MsgBox(strMsg, vbQuestion + vbYesNoCancel, "Save Record?")
    Case Is = vbYes
        'MsgBox "Yes"
        'do nothing
    Case Is = vbNo
        'MsgBox "No"
        DoCmd.RunCommand acCmdUndo
        CancelYes = False
    Case Is = vbCancel
        'MsgBox "Cancel"
        CancelYes = True
End Select
End If
If CancelYes <> True Then
    If ParentFormIsOpen() Then Forms![Employees1]!ToggleLink = False
Else
    'MsgBox ("This will stop it from closing")
    DoCmd.CancelEvent
    CancelYes = False
End If
Form_Unload_Exit:
    Exit Sub

Form_Unload_Err:
    MsgBox Error$
    Resume Form_Unload_Exit

End Sub
so that whenever the form is closed it checks to see if data has changed and ask if that is what you want to do problem is that when I hit esc it closes the form and ignrs any changes that where made

but when I click on the close button or the button I created for close
Code:
Private Sub CloseForm_Click() 'Button
    On Error GoTo Err_CloseForm_Click
    DoCmd.Close

Exit_CloseForm_Click:
    Exit Sub

Err_CloseForm_Click:
    MsgBox Err.Description
    Resume Exit_CloseForm_Click
    
End Sub
it works fine any thoughts?

oh and Form.Refresh before cmd.close doesnt work
 
You dont need the

Private Sub Form_KeyPress code.

In the properties of the Close button is a property called cancel. When you set this to yes, when the Esc key is pressed the button code is run.

Dave
 
Oldsoftboss said:
You dont need the

Private Sub Form_KeyPress code.

In the properties of the Close button is a property called cancel. When you set this to yes, when the Esc key is pressed the button code is run.

Dave

Can you maybe upload a screenshot of what you are looking at as I am not able to find the same property I see property of close button and a choice of yes or no??? :confused:
 
Have a look at the properties of the command buttons
 

Attachments

  • cmdbutton.JPG
    cmdbutton.JPG
    67.4 KB · Views: 152
The highlighted properties:

Cancel will enable the Esc key.

Default will enable the button to be run when you hit the Enter key. Handy on a logon button or save button.

Dave
 

Users who are viewing this thread

Back
Top Bottom