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