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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem saving record, closing form when key violation, error 2757

Status
Not open for further replies.

pkahlo

Programmer
Nov 3, 2003
29
US
I am in the process of coverting a jet MDB to an ADP. I've noticed some weird behavior in regards to saving data on a form.

My forms are closed using a command button that basically does this:

Code:
    If frm.Dirty Then
        frm.Dirty = False
    End If
    DoCmd.Close acForm, frm.Name

This normally works. I've noticed that when there is a primary key violoation, I get this error:

Run-Time error '2757':
There was a problem accessing a propery or method of the OLE object.

I also get this error if I try to run: "RunCommand acCmdSaveRecord"

However, if I close the form manually (not using the button) or save using the Menu (Records->Save Record) I would get the expected msg box stating there is a primary key violation and you cannot save this record.

Why don't I get a message like that when I run this stuff from code?

If I just run "DoCmd.Close acForm, frm.Name" the form will close and NOT prompt that there is a key violation and it will not save. So that isn't good either.

How do I close a form using a button so that is prompts about a key violation without giving strange errors or just closing with no warning?
 
You should be able to catch the key violation error in the afterupdate method of the Form. Personally, I avoid using Macro's unless necessary because of the type of error you are encountering. Look up the scope of error handling on a Form. You should be able to code error handling logic at the Form level.
 
I don't use any macros in this database. And yes, I can catch the error, but why isn't giving me a more specific error to catch?

Thanks!
 
Also, I will get other errors when accessing "frm.dirty" during the closing of a form for other reasons (and most of the time I'm not sure why, the example in my first post is the only time I can consistenly generate an error). Most of them give me the error: Run-time error '2455': You entered an experssion that has an invalid reference to the property Dirty.

So basically I'm asking, how do you close a form using a button in an ADP without all these weird errors?

Thanks,
 
This is a Macro DoCmd.Close. The DoCmd runs Macros.

Does your error handling look something like?

Public Function Errorhandling()
On Error GoTo errorhandler

errorhandler:
For Each Err In Errors
Debug.Print Err.Number
Debug.Print Err.Description
Next

End Function

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top