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

Runtime Error 2501 on Cmd.Save

Status
Not open for further replies.

sheusz

Technical User
Jan 21, 2008
24
AU
Hi All,

I have a database that is run across a server that used to work fine. Recently I added the following code to one on the data entry forms;

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Provide the user with the option to save/undo
'changes made to the record in the form

    If MsgBox("Changes have been made to this record." _
        & vbCrLf & vbCrLf & "Do you want to save these changes?" _
        , vbYesNo, "Changes Made...") = vbYes Then
            [COLOR=Yellow][b]DoCmd.Save[/b][/Color]
        Else
            DoCmd.RunCommand acCmdUndo
    End If
End Sub

The form work fine if there is only one user accessing the database, but as soon another user uses the database, then tries to to save a record on the form I get the following error;

Run-Time error '2501'
The Save action was canceled.


Checking the Debug Screen highlights the line
DoCmd.Save


If I remove the above code everything works fine.

We are using MS Access 2003.

Does anybody have a clue as to what I an doing wrong here?

Thanks in advance
 
How are ya sheusz . . .

Be aware: Whenever you use the [blue]BeforeUpdate[/blue] event of a form or control, the logical attribute of the event is to abort if validation isn't proved! . . . With this in mind ,try the following:
Code:
[blue]   Dim Msg As String, Style As Integer, Title As String, DL As String
   
   DL = vbNewLine & vbNewLine
   Msg = "Changes have been made to this record." & DL & _
         "Do you want to save these changes?"
   Style = vbQuestion + vbYesNo
   Title = "User Response Required! . . ."
   
   If MsgBox(Msg, Style, Title) = vbNo Then
      Cancel = True
      [purple][b]Me.Undo[/b][/purple]
   End If[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Do not think this will help, but docmd save without the parameters is a little sloppy. You may get unintended consequences. I would specifically specify what you are saving

docmd.save acform, "frmName"
 
Hi AceMan1.
Thanks for that. It's solved the problem and also the clue on BeforeUpdate!.

Also thanks to MajP for the heads up on style.
 

Aceman1's code is, of course, the way do this, but shouldn't someone explain to the OP that

DoCmd.Save

is used to save objects, not records, and that

DoCmd.RunCommand acCmdSaveRecord

or

If Me.Dirty Then Me.Dirty = False

is used to save records?

Shouldn't we also explain that issuing a command to save a record in the Form_BeforeUpdate event will also pop an error and actually prevent the record from being saved because it was already in the process of being saved?

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Thanks Missinglinq

for the clarification. Have to admit that I wasn't fully aware of the subtle difference. Now I need to check lots of other forms!
 

It's a commonly made mistake. It would have just been wayyyyyy too hard for Micro$oft to have named the command something lucid like SaveObject instead of simply Save!

And while we're on the subject

DoCmd.Close acForm, "YourFormName", acSaveYes

is the same trap! The acSaveYes saves the form if its design has been changed, not the record/data!



The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Hi missinglinq,

As this is a commonly made mistake, should this be made as a FAQ?

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top