Runtime 2501 - Save action was cancelled

ljoekelsoey4

Registered User.
Local time
Today, 18:32
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
 
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
 
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
 
you must also do this befire closing the form when the answer is Yes, to save the record:

me. dirty=false
 
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.
 
Me.Dirty = False forces a save if the record has any unsaved changes.
 
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
 
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.
 

Users who are viewing this thread

Back
Top Bottom