Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to save record from forms On Key Press event? 1

Status
Not open for further replies.

oticonaus

Technical User
Dec 16, 2003
96
AU
I have a form bound to a table, then filtered to show 1 record.

If I change a field on the form, and close the form with the x, the changed data saves to the table.

If I close the form programmatically from the OnKeyPress event, it doesn't save.

Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyEscape Then
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close acForm, "frm Customer Card"
    End If
End Sub

Strangely, if I select Records > Save Record, then close, it does save....

What am I missing?
 
Try it this way
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = 27 Then
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close acForm, "frm Customer Card"
    End If
End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
My father was a realistic father; not a vending machine dispense everything I demanded for!!
 
I wasn't clear - the KeyPress event fires and the form closes as expected - it's just that the edit's to the record don't get saved.
 
And this ?
If KeyAscii = vbKeyEscape Then
KeyAscii = 0
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acForm, "frm Customer Card"
End If

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
No PHV it will defenitely undo the record changes. Evenif you set the Dirty=False

oticonaus,
What I am saying is to remove the "KeyPress Event" and use the "KeyDown Event".

________________________________________________________
Zameer Abdulla
Help to find Missing people
My father was a realistic father; not a vending machine dispense everything I demanded for!!
 
OK - I have now moved the code from the KeyPress event to the KeyDown event. Now, when pressing Esc on the Form, changes are undone, and the form does not close. (So it's worse!)

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyAscii = vbKeyEscape Then
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close acForm, "frm Customer Card"
    End If
End Sub
 
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    [s] If KeyAscii = vbKeyEscape Then[/s]
    If KeyCode = 27 Then
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close acForm, "frm Customer Card"
    End If
End Sub

________________________________________________________
Zameer Abdulla
Help to find Missing people
My father was a realistic father; not a vending machine dispense everything I demanded for!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top