exiting form from keypress event

rwilson32

Registered User.
Local time
Today, 01:07
Joined
Jul 10, 2003
Messages
23
I have a form that I want to close when the users presses the 'esc' key. It work fine but none of the information that I have entered on the form is saved. When I reopen the form I don not see any of my data. Here is my code:

Private Sub Form_KeyPress(KeyAscii As Integer)

If KeyAscii = 27 Then
DoCmd.Close acForm, "frminformation"
End If

End Sub

Any help would be much appreciated. Thanks.
 
I tried doing the refresh but it doesn't help. I don't know if it matters but my tables are linked tables to an odbc data source. It won't even save if I click on the save icon before I press the esc key. Thanks for the reply.
 
To tell you the truth, I had this problem before.. I just dont know how I fixed it :( I tried finding my database that had the solution, but no luck.

I think there was a problem with the focus, like I had to set the focus to an object on the form before I closed. In any event, you could always create a new record, which the database will ignore.
 
It is a good idea to separate data entry and data view, either by using different forms or by switching functions on the same form. Sort out your required fields and validations first. You should really ask the user whether they want to save or not:
Code:
    Select Case MsgBox("Do you want to save record?", vbYesNoCancel, "Save Record?")
        Case vbYes
                DoCmd.GoToRecord , , acNewRec
                DoCmd.Close
        Case vbNo
                DoCmd.Close
        Case vbCancel
                'do nothing
    End Select
 
Thanks for the replies. I still don't quite understand how to fix my problem though. I don't see how creating a new record is going to help anything. I just need it to save what has been entered on the form's existing record before it closes. Thanks again for the help.
 
Thanks for the help everybody, but I figured it out. If you run the function on the keydown event rather that the keypress event it works just fine. Thanks for all your help
 
rwilson32 said:
Thanks for the help everybody, but I figured it out. If you run the function on the keydown event rather that the keypress event it works just fine. Thanks for all your help

I'm not sure, but I still think this goes back to the issue about losing its focus. Glad you found your answer though :)
 

Users who are viewing this thread

Back
Top Bottom