Runtime 2501 - Save action was cancelled (1 Viewer)

ljoekelsoey4

Registered User.
Local time
Today, 11:53
Joined
Mar 16, 2017
Messages
49
Getting the above error very intermittently from users. Occuring when users click 'Save and Close', there is also no save close button. I'm unable to replicate the error on my end. :confused:

Code:
Private Sub cmdClose_Click()
Dim Response As Integer
Response = MsgBox(Prompt:="Do you want to save this record?", buttons:=vbYesNo)
   If Response = vbYes Then
    Me.EventEntryDate = Me.datetoday
    Me.EventEntryTime = Me.timenow
    MsgBox "Saved"
    DoCmd.Close
    Exit Sub
   Else
    MsgBox "Changes have been cancelled"
    Cancel = True
    Me.Undo
    DoCmd.Close
    Exit Sub
    End If
End Sub

Private Sub cmdCloseNoSave_Click()
Dim Response As Integer
Response = MsgBox(Prompt:="This record will not be saved. Are you sure you want to close?", buttons:=vbYesNo)
   If Response = vbYes Then
      Cancel = True
      Me.Undo
      DoCmd.Close
      Exit Sub
   Else
      Cancel = True
   End If
End Sub
 

MarkK

bit cruncher
Local time
Today, 03:53
Joined
Mar 17, 2004
Messages
8,195
This code does not initiate a save action. I am very doubtful that that error is generated by this code. There are plenty of other problems with this code, for instance, what does...
Code:
Cancel = True
...do in this context? Cancel is not declared anywhere, and the events in which it appears cannot be cancelled. There are also way too many MsgBoxes, for instanct this...
Code:
    MsgBox "Saved"
...is just a nuisance. You are forcing your user to clear a message box that tells them something they already expect to be true.
Also, if both the true and the false part of the if block will close the form and exit the sub, then there is no point duplicating that fact in both blocs, so change...
Code:
Private Sub cmdClose_Click()
Dim Response As Integer
Response = MsgBox(Prompt:="Do you want to save this record?", buttons:=vbYesNo)
   If Response = vbYes Then
    Me.EventEntryDate = Me.datetoday
    Me.EventEntryTime = Me.timenow
    MsgBox "Saved"
[COLOR="blue"]    DoCmd.Close
    Exit Sub[/COLOR]
   Else
    MsgBox "Changes have been cancelled"
    Cancel = True
    Me.Undo
[COLOR="Blue"]    DoCmd.Close
    Exit Sub[/COLOR]
    End If
End Sub
...to ...
Code:
Private Sub cmdClose_Click()
Dim Response As Integer
Response = MsgBox(Prompt:="Do you want to save this record?", buttons:=vbYesNo)
   If Response = vbYes Then
      Me.EventEntryDate = Me.datetoday
      Me.EventEntryTime = Me.timenow
   Else
      Me.Undo
   End If
[COLOR="Blue"]   DoCmd.Close acForm, Me.Name[/COLOR]
End Sub
Also, I would store date and time in the same field, and rather than do this in a form, consider setting date/time added using the Default Value property of a field in the table itself. That way it doesn't matter if the record is added in the form or not, the table will allways record, automatically, when the row was created.
hth
Mark
 

ljoekelsoey4

Registered User.
Local time
Today, 11:53
Joined
Mar 16, 2017
Messages
49
That's me rumbled as a rubbish coder then ;)

Thanks for the tips, I'll try these out and see if it improves the situation.

One thing to ask though, when closing acForm, the form in question doesn't appear in the list... is there a reason why this is preferred to a simple DoCmd.Close ?

Thanks
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:53
Joined
May 7, 2009
Messages
19,249
you must also do this befire closing the form when the answer is Yes, to save the record:

me. dirty=false
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 18:53
Joined
May 7, 2009
Messages
19,249
for Both, when the answer to the msgbox is Yes.
you don't want to loose changes to the record when you close the form.
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 06:53
Joined
Oct 17, 2012
Messages
3,276
Me.Dirty = False forces a save if the record has any unsaved changes.
 

MarkK

bit cruncher
Local time
Today, 03:53
Joined
Mar 17, 2004
Messages
8,195
If the form is bound, there is no need to explicitly save the record as arnelgp states. In a bound form, access saves the current record automatically when you move to a new record or when close the form.
Mark
 

Frothingslosh

Premier Pale Stale Ale
Local time
Today, 06:53
Joined
Oct 17, 2012
Messages
3,276
Also true...unless you want to pop up a message box with 'Saved' after the save is complete but before the form closes. :)

Personally, if I need to do that, I have a different (tiny) form I just open that only says 'Record save complete', then closes after 2 seconds.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:53
Joined
Feb 19, 2002
Messages
43,626
The 2501 error is caused when one procedure executes another and the called procedure does not finish correctly. So, the DoCmd.Close causes the form's BeforeUpdate event to run if the current record is dirty. If the save fails, an error is raised to the the calling procedure. You can fix the problem by adding an error trap in cmdClose_Click() which checks for 2501. You can safely ignore this error because the BeforeUpdate event should have already displayed an error message.

There are other issues with the code. I made comments in line.
Code:
Private Sub cmdClose_Click()
Dim strResponse As Integer
    On Error GoTo ErrProc
    If Me.Dirty = False  '' if the form is not dirty, just close and exit.  No save is necessary
        DoCmd.Close
        Exit Sub
    End If
    strResponse = MsgBox(Prompt:="Do you want to save this record?", buttons:=vbYesNo)
    If strResponse = vbYes Then
        DoCmd.RunCommand acCmdSaveRecord
        DoCmd.Close
    Exit Sub
    Else
        MsgBox "Changes have been cancelled"
        '''''Cancel = True  --- you can't do this in an event that doesn't support the Cancel argument.
        Me.Undo
        DoCmd.Close
        Exit Sub
    End If

ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case 2501 '' save was not completed
            Resume ExitProc
        Case Else
            Msgbox Err.Number & " -- " & Err.Desc
            Resume ExitProc
    End Select
End Sub

'''''this code belongs in the Form's BeforeUpdate event. Not in this one. There are a number of situations that will cause Access to want to save a record. You want this code to be executed regardless of what prompted Access to save. This is why the bulk of your validation code belongs in the Form's Beforeupdate event. The validation code MUST run regardless of what causes the record to be saved. Not just when someone presses your button. Personally I rarely use save or close buttons. When I do, the code just saves the record. ALL the question prompts belong in the Form's BeforeUpdate event because you want them to run REGARDLESS of what prompted the save. Test your form. Dirty a control and press the next record arrow. Access will save your record and NONE of your messages will pop up. So although I fixed the biggest part of the problem for you, i didn't fix all the problem.
Code:
        Me.EventEntryDate = Me.datetoday
        Me.EventEntryTime = Me.timenow
        '' MsgBox "Saved
 
Last edited:

Users who are viewing this thread

Top Bottom