Form Save button and Forms before update event

DevAccess

Registered User.
Local time
Yesterday, 23:09
Joined
Jun 27, 2016
Messages
321
Hello there,

I have form in which I have save button which has code as below :

Code:
docmd.runcommand acsaverecord

and form's before update event has code as below to confirm users response whether they want to save changes or not, if they decided not to save changes, on VBNo we are putting code as cancel= true, in this code gives error saying save command not available and it points to the code of the button docmd.runcommand acsaverecord.

What would be possible way this could be happenin, am I doing anything wrong here ?
 
The save button is not even needed. Access saves automatically when you leave the record.
Why re-invent the wheel?
 
The save button is not even needed. Access saves automatically when you leave the record.
Why re-invent the wheel?

We as technical person can understand it saves automatically when you leave away with record, but end users need save button which is the requirement. and on before update event I want to make sure when they cancel the record should not be saved.
 
The error will be thrown if there are no changes to save.
 
The error will be thrown if there are no changes to save.

I want to have save button which should fire on before update event of the form ...

and check for if user want to save the data or not if not it should goto form's button event

docmd.runcommand acsaverecord.

My concern is that on form before update event if user selects No then I am putting cancel=true, but that gives me error on buttons click event that save is not available

doccmd.runcommand acsaverecord ''here it gives error
 
Check the Me.Dirty property (True/False) before attempting the save. Don't actually perform the save operation if the form isn't dirty.
 
If you want this kind of control over the update of the table I'm afraid you will have to go with unbound forms and do the updates in VBA. I believe any event that moves focus from the form is going to update the table. Setting cancel = true in the beforeupdate will just trap you in the form. I believe the only way you could allow the users to discard changes is to trap all the events (on current, on close,etc) and give the user the option of discarding changes which would be enacted with Me.Undo.
 
Check the Me.Dirty property (True/False) before attempting the save. Don't actually perform the save operation if the form isn't dirty.

is this same as

on error goto errorhanlder

docmd.runcommand acsaverecord

exit sub
errorhanlder:
msgbox err.description
exit sub
 
is this same as

on error goto errorhanlder

docmd.runcommand acsaverecord

exit sub
errorhanlder:
msgbox err.description
exit sub

Not really. Testing for Dirty before saving will quietly just do what it has been ask.
 
Code:
If Me.Dirty Then
    DoCmd.RunCommand acSaveRecord
End If

However, there is another thing to consider - the "BeforeUpdate" event CAN be canceled. If you DO attempt to navigate, canceling the update would stop navigation and would also stop closure of the form.

If you really want to protect against "silent" update, you need to trap both the Form_Close and Form_BeforeUpdate events with similar tests:

Us Me.Dirty in a test to see if you need to even ask the question. If you do, use a MsgBox function with the "Yes/No" option to ask the user if s/he really wants to save the record. (Look it up online if you have not used it before.) Then capture the answer for the message box. If the user said, "Yes, save it" then do nothing else... but if the user said, "No, don't save it" then do the cancel for those two events, BOTH of which allow you to cancel.
 
In post #8 I asserted that you could negate these automatic updates with Me.Undo. For a moment I though that you could simple negate all of these update by putting Me.Undo in the afterupdate but I just tried that and it doesn't work. Sorry if I led you astray.
 
Man am I stupid today. Of course Me.Undo isn't going to do anything in the afterupdate. By then its to late. However to my surprise you can put Me.Undo in the beforeupdate where it actually does something. I tested this in the attached database where in the beforeupdate I have:

Code:
If Me.Dirty Then
    If MsgBox("Do you want to save changes", vbYesNo) = vbNo Then
        Me.Undo
    End If
End If

This give the user the option of discarding changes anytime they would be saved which mean the message shows up for any of those events, e.g., clicking the Save button, navigating to another record or closing the form. Can you live with that?
 

Attachments

Man am I stupid today. Of course Me.Undo isn't going to do anything in the afterupdate. By then its to late. However to my surprise you can put Me.Undo in the beforeupdate where it actually does something. I tested this in the attached database where in the beforeupdate I have:

Code:
If Me.Dirty Then
    If MsgBox("Do you want to save changes", vbYesNo) = vbNo Then
        Me.Undo
    End If
End If

This give the user the option of discarding changes anytime they would be saved which mean the message shows up for any of those events, e.g., clicking the Save button, navigating to another record or closing the form. Can you live with that?

Do you mean I can put me.undo instead of cancel =true..

also what if I want to check other criteria before saving the record.
 
Your save and cancel buttons need to be on section(0) of the form, not a header or footer or something.
So a continuous form would show the same 2 buttons for every record.
Otherwise clicking ouside of the record (the cancel button) would save the record.

Code:
Private Sub btnSave_Click()
    Me.Dirty = False
End Sub

Private Sub btnCancel_Click()
    Me.Undo
End Sub
 
Do you mean I can put me.undo instead of cancel =true...
It seems to work. cancel = true will inhibit any navigation or closing of the form so I don't think that will work. You can still used it as shown in the code below. You can test this in the attached database.

Code:
If Me.F2 > 9 Then
    MsgBox "F2 must be less than 10"
    Me.F2.SetFocus
    Cancel = True
    Exit Sub
End If


If Me.Dirty Then
    If MsgBox("Do you want to save changes", vbYesNo) = vbNo Then
        Me.Undo
    End If
End If

In the case where the user enters 10 or more in F2 he is locked into the form until he corrects it. Of course you could add a message box to this to undo the change at this point too.
 

Attachments

Users who are viewing this thread

Back
Top Bottom