I have an Access 2000 database with a form that is giving me some
major headaches. When you open the form, it displays all records and
allows editing, but has AllowAdditions set to False so that the user
has to use my New Record button. When you click the New Record
button, the form presents a new record for editing. My client wants
to use the Escape key to cancel changes to new or existing records.
On existing records, Access already handles this - hitting the Escape
key cancels changes and redisplays the record without your changes.
However, on a new record, hitting the Escape key in form view doesn't
do anything. So, I'm trapping the Escape key in the Form_KeyDown
event, and then calling some code to discard the new record and return
the form to view/edit mode.
Here's where things get interesting. If you open the form, click the
New button, and then hit the Escape key, the form does everything it's
supposed to do, but as soon as you move to a different record with the
navigation keys, you get the error message "Object invalid or not
set". Actually the message pops up twice, one right after the other.
This behavior happens very consistently.
Now, after digging through the Microsoft knowledge base, searching
through all the newsgroup postings on this error, decompiling my
database, and installing the latest service pack, I still haven't
found the solution, but I have found a curious workaround.
If I instead put a Cancel button on the form instead of trapping the
Escape key, and run the exact same code when it's clicked, the error
goes away. So, why not just leave it that way, you ask? Well, I just
may if my client can bend a little. But it's a little scary to think
that the Escape key actually caused the problem. I'm concerned that
something else is to blame here and that it may manifest itself under
different circumstances.
Here's some code in case anyone has some ideas on why the error occurs
when I trap the Escape key:
' Undo all record editing and switch to view/edit mode if we are in
new record mode.
Private Sub CancelChanges()
If mbooNewRecord Then
Me.Recordset.CancelUpdate
mbooNewRecord = False
Me.Recordset.MoveLast
' Synchronize the form bookmark with the recordset bookmark.
Me.Bookmark = Me.Recordset.Bookmark
ViewModeSetup
End If
End Sub
' Configure buttons for new record mode.
Private Sub NewModeSetup()
' Move focus off the New button before disabling it.
[Cust Name].SetFocus
' Clear these unbound combo boxes.
[Cust Name].Value = ""
[Client Name].Value = ""
[Referral Name].Value = ""
[Referral Name].Enabled = False
[Water Type].Enabled = False
cmdNew.Enabled = False
cmdJobList.Enabled = False
cmdDuplicate.Enabled = False
cmdRelatedJobs.Enabled = False
Me.NavigationButtons = False
End Sub
' Configure buttons for record viewing/editing mode.
Private Sub ViewModeSetup()
cmdNew.Enabled = True
cmdJobList.Enabled = True
Me.NavigationButtons = True
Me.AllowAdditions = False
End Sub
Private Sub cmdNew_Click()
DoCmd.RunCommand acCmdSaveRecord ' Save any pending changes
first.
NewModeSetup
Me.AllowAdditions = True
mbooNewRecord = True
Me.Recordset.AddNew
End Sub
The error occurs when this code is executed in response to hitting the
Escape key:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim lctlCurControl As Control
' F2 key activates the contact view/edit form.
If KeyCode = vbKeyF2 Then
ViewContact
' F3 key activates the contact list.
ElseIf KeyCode = vbKeyF3 Then
On Error GoTo NoActiveControl
Set lctlCurControl = Screen.ActiveControl
If lctlCurControl.Name = "Client Name" Then
Client_Lookup
ElseIf lctlCurControl.Name = "Referral Name" Then
Referral_Lookup
Else
NoActiveControl:
Customer_Lookup
End If
ElseIf KeyCode = vbKeyEscape Then
KeyCode = 0 ' Cancel the keystroke.
CancelChanges
End If
End Sub
The error doesn't occur when the following code is run is response the
clicking the Cancel button:
Private Sub cmdCancel_Click()
CancelChanges
End Sub
Also note that I've tried creating and discarding the new record using
DoCmd.RunCommand and DoCmd.MenuItem without any change in behavior.
I've also tried not cancelling the Escape keystroke in the KeyDown
event. In reference to knowledge base articles, I'm not attaching to
any Oracle tables, and I'm always setting a local variable to
CurrentDb before referencing any properties or methods of it.
Any thoughts on the problem are greatly appreciated.
Paul