Save file?

Klow

New member
Local time
Today, 21:04
Joined
Feb 9, 2008
Messages
8
Hi guys, little question.

I really like Access, I can manage well with it. But I have a problem, and I really wonder why this kind of thing happens in such a professional program using a lot of important data.

Actually the program saves automatically, and I don't like it. For example I try a little something, delete some stuff, replace by something else, and when I quit the program, it doesn't say a thing, and when I come back, the unimportant stuff I changed is still there, and what was replaced is lost. Why? Is there any way to force the program to ask if I want to save the changes?... It's really annoying and I really don't feel secure with my data with this system.

Thanks for your clues! :)
 
You need to use the Before Update event to determine if you want to save changes and then issue a Cancel = True if you don't.
 
Interesting, and where do I find this option?...
 
Mh, it seems a bit tricky. Where do I go to do this??
 
In the Form properties Before Update event.
You will have to put something like this as the code (the top and bottom lines will already be there - don't duplicate them)

Private Sub Form_BeforeUpdate(Cancel As Integer)

'Option to save/undo changes to the record

If MsgBox("Changes have been made to this record." _
& vbCrLf & vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
DoCmd.Save
Else
DoCmd.RunCommand acCmdUndo
End If
End Sub
 
In the Form properties Before Update event.
You will have to put something like this as the code (the top and bottom lines will already be there - don't duplicate them)

Private Sub Form_BeforeUpdate(Cancel As Integer)

'Option to save/undo changes to the record

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

Bob - You don't need this line:
DoCmd.Save
as it has nothing to do with records (it has to do with form design changes).

Also, you must use the Cancel=True before the undo command so that you cancel the update. If you do not use the Cancel=True it will still commit the changes.

The command to save a record is either:

DoCmd.RunCommand acCmdSaveRecord

or

If Me.Dirty Then Me.Dirty = False


But, you can't put a save command in the before update event because it is already in the process of saving.
 
Aaaah it's so abstract to me! Could you just put only the code I have to put in the form properties? And when I get to these properties the only field available is "description". Is that right?...
 
Aaaah it's so abstract to me! Could you just put only the code I have to put in the form properties? And when I get to these properties the only field available is "description". Is that right?...
 
See, I try to help someone and I just mislead them. :(
Heck Bob, I've been using that and thought it was working. At least it has been saving when I hit 'y'. Shoulda looked at whether it was on 'n'.
You got me confused on the Cancel=True bit though. :confused:


I went back and tried it several times in different forms using yes and no. Seems to be working ok.
Now I'm really confused.
 
Last edited:
If you notice, the event shows as

Private Sub Form_Before_Update(Cancel As Integer)

And the way the before update event works, is that if you want to cancel it you set the Cancel integer to something other than False (0). False evaluates to zero (0) and True, while officially is listed as -1, it actually will work for any non zero number. But, typically you would set it equal to TRUE (which evaluates to -1 in Access VBA).

The easy thing to remember is if you want to cancel the update you tell it

Cancel = True

If you don't want to cancel, you don't have to set it to False because it is already set to False by default.

As for your code working when you click NO, it would work if you are putting it in the Before Update event of a control, but I wouldn't think it would work right if used in the before update event (or if it did, I would think an error would occur which would then need to be handled). But, if it does work, I guess that's okay. But, I personally wouldn't count on it without explicitly telling Access to cancel the update.
 
Private Sub Form_BeforeUpdate(Cancel As Integer)

'Option to save/undo changes to the record

If MsgBox("Changes have been made to this record." _
& vbCrLf & vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
DoCmd.RunCommand acCmdSaveRecord
Else
Cancel=True
End If
End Sub

So, is this correct ?
or should the vbYes/No = vbNo Then ?

oops

Tried this - breaks on the DoCmd.RunCommand acCmdSaveRecord
 
Last edited:
So will you guys come up with an agreement one day? :D

When you're done, could you please tell me exactly what code I must add and exactly where? I'd be so nice from you.

I really wonder why this is not enabled by default in the program...
 
klow,

this will cancel the save

Code:
'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...") = vbNo Then
            Cancel = True
            DoCmd.RunCommand acCmdUndo

End If
 
Last edited:
Great! And know would you tell me precisely where to paste this, and we're done! :D
 
open the form in design view
right click on the form (not a field) then in the properties box make sure it is on Form.
then search in the list for Before Update and click on the ... out to the right, choose Code
copy and paste this code IN BETWEEN the Private subform statement and the End sub

Then just close the form and save it

should look like:

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...") = vbNo Then
            Cancel = True
            DoCmd.RunCommand acCmdUndo
End If

End Sub
 
Ok I did it. I found where to put it and all. But it doesn't work. I still can close the program without any message.
 
Can you post a copy of your database with any sensitive information removed?
 

Users who are viewing this thread

Back
Top Bottom