By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,469 Members | 2,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,469 IT Pros & Developers. It's quick & easy.

"Object invalid or not set" error message - related to trapping the Escape key???

P: n/a
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
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Paul" <pa*******@comcast.net> wrote in message
news:9c**************************@posting.google.c om...
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.


You lost me at this point, Paul.

The Esc key (or Undo from the Edit menu) should undo a new record.
It might be good to see why this is not happening.
You should also get the Form_Undo event at this point.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.
Nov 12 '05 #2

P: n/a
Allen,

Thanks for your reply. You're right - the Escape key does undo changes
to the new record, but it leaves you on a blank form. My intention here
is to take the user out of the new record completely and return to the
last record in the recordset.

Also, I don't see that there's any such thing as a Form_Undo event. Am
I missing something here?

Paul

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #3

P: n/a
Rats! You said Access 2000. Form_Undo was introduced in 2002.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Paul Angelino" <pa*******@comcast.net> wrote in message
news:3f*********************@news.frii.net...
Thanks for your reply. You're right - the Escape key does undo changes
to the new record, but it leaves you on a blank form. My intention here
is to take the user out of the new record completely and return to the
last record in the recordset.

Also, I don't see that there's any such thing as a Form_Undo event. Am
I missing something here?

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.