How to create a save and go to new record button without affecting Me.Undo??

xwnoob

Registered User.
Local time
Today, 12:27
Joined
Nov 7, 2011
Messages
70
How do i create a button that saves the current record and moves to a new one? I want to do it in such a way that microsoft access acknowledges the button.

Below is the following codes in my form:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
'Ensure that users do not accidentally save records
If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
Me.Undo
End If
End If
End Sub
Private Sub btnUndoIndividualBorrower_Click()
'clicking on this will undo changes
Me.Undo
End Sub

Private Sub Form_Current()
' Disable undo changes button
Me!btnUndoIndividualBorrower.Enabled = False
End Sub
Private Sub Form_Dirty(Cancel As Integer)
'enable changes button
Me!btnUndoIndividualBorrower.Enabled = True
End Sub



I tried to create save and go to new record using macros and the wizard but im not sure if they even work because everytime i press the button my beforeupdate event appears. How can modify the code such that when i press the button, it savees the record and goes to a new one without displaying my "Do you want to save updates?" message.
 
I will give you two hints:

1. The BeforeUpdate event of the form will only fire if the form is Dirty so there's no need to check whether it's dirty in that event.
2. To Cancel the update you set Cancel to True. Can you see the Cancel argument in the BeforeUpdate event?
 
I will give you two hints:

1. The BeforeUpdate event of the form will only fire if the form is Dirty so there's no need to check whether it's dirty in that event.
2. To Cancel the update you set Cancel to True. Can you see the Cancel argument in the BeforeUpdate event?

Yes is it the cancel before integer? So what do i do with it. Can you give me an example of what i should do and how it relates to the button that i want to create.
 
So based on the information I provided I would imagine that you've changed your code. Let me see your new code. Enclose it in code tags -->

http://www.access-programmers.co.uk/forums/showthread.php?p=1009015#post1009015


Alright i have added in the in code that you gave me. what im going to do now is add a new record button using the wizard. Hope it works

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
'Ensure that users do not accidentally save records
If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
Me.Undo
If Msgbox(...) = vbNo Then
[COLOR=red]Cancel = True[/COLOR]
End If
End If
End If
End Sub
 
Last edited:
Is that really the code that you added? You made no other change to it?

Plus, I've already mentioned twice (i.e. this post and your other post) that you should NOT be testing Me.Dirty but you still are.
 
Is that really the code that you added? You made no other change to it?

Plus, I've already mentioned twice (i.e. this post and your other post) that you should NOT be testing Me.Dirty but you still are.


I only added the code you gave me.Ok I just want to add my reason for doing this.My form has 2 subforms on it and i intend to add a new record button to each of them so that the only way you can save the records are to click on the buttons.Or maybe i would just add a save button and new record button separately.

I've only just started using ms access for awhile now so can you explain why me.dirty should not be added.Because im trying to prevent changes to accidental modifications or accidental saves but if the user wants to intentionally save the record he would click this save button/new record button
 
In my first post, point 1, I mentioned that the form will already be dirty when the BeforeUpdate event fires. That means Dirty will always be true in that event. Makes sense?

The form because dirty once you edit a record and the BeforeUpdate of the form fires just before the record is saved to the table.

So your code should simply be this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Ensure that users do not accidentally save records
    If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
        Cancel = True
    End If
End Sub
Cancel = True means stop the changes.

I hope that helps!
 
In my first post, point 1, I mentioned that the form will already be dirty when the BeforeUpdate event fires. That means Dirty will always be true in that event. Makes sense?

The form because dirty once you edit a record and the BeforeUpdate of the form fires just before the record is saved to the table.

So your code should simply be this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Ensure that users do not accidentally save records
    If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
        Cancel = True
    End If
End Sub
Cancel = True means stop the changes.

I hope that helps!

Ok but how do i add an exception to new records. SInce this code fires whenever the form is dirtied, then this would be a problem as new records are not accidental and are to be added in intentionally.

Edit:My previous code works the same as your code right? If so, the sub will surely affect new records
 
Test against Me.NewRecord

Your code is not the same as mine.
 
Ok so i just have to add the following code in right?Ill try it now
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Ensure that users do not accidentally save records
   [COLOR=red]If me.newrecord then exit sub[/COLOR]
    If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
        Cancel = True
    End If
End Sub
 
It depends on what you would like to happen if it's a new record?
 
It depends on what you would like to happen if it's a new record?

Then i would like the sub to not affect the record and go ahead with giving the msgbox. Anyways, i tested the code and it didnt undo the changes when i purposed click to another subform so i went to check your code...it did not have me.undo. So i added it in under the cancel true i


Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Ensure that users do not accidentally save records
   If Me.NewRecord Then Exit Sub
    If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
        Cancel = True
        Me.Undo

    End If
End Sub
 
Your code should do it. But this is the format I would use (instead of exit sub):
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Ensure that users do not accidentally save records
    If Not Me.NewRecord Then
        If MsgBox("Do you want to save the updated records?", vbOKCancel, "Save?") = vbCancel Then
            Cancel = True
            Me.Undo
        End If
    End If
End Sub
 
hmm i think the code did not work the way i wanted. I think this code still has problems.

The problem that still happens is that whenever i press the add new record button, the message prompt still appears. I just want the messagebox not to appear when i press this add new record button because i want access to acknowledge that user wants to save message and its not accidental...

Additionally, i tested the code and now its worst as when im entering a new record, i am able to enter new records in the subform without entering any info in the main form.The only good thing about this is that the ADD NEW RECORD button does not have any prompt...but i need this prompt when moving from subform to form/accidentally pressing shift+enter/closing ms access.(whatever accidental methods of saving)

Previously, whenever i move to the subform from the mainform after editing data, there would be a prompt. In the new code, this only happens when i go back to the saved record( i know it happens because its a old record so the new record if statement does not apply), but both these methods still have prompts when clicking the button.


This is frustrating...maybe i shouldnt give a crap about this kind of messages. Im only doing this in case my users save the records accidentally but it just does not seem worth it to spend so much time.
 
Last edited:
The problem that still happens is that whenever i press the add new record button, the message prompt still appears. I just want the messagebox not to appear when i press this add new record button because i want access to acknowledge that user wants to save message and its not accidental...
Of course if you were editting a record and then pressed the New Record button it will prompt you because it has not yet gotten into a new record. If you were not editting a record and you press the New Record button it will not prompt you.

Additionally, i tested the code and now its worst as when im entering a new record, i am able to enter new records in the subform without entering any info in the main form.The only good thing about this is that the ADD NEW RECORD button does not have any prompt...but i need this prompt when moving from subform to form/accidentally pressing shift+enter/closing ms access.(whatever accidental methods of saving)
Nothing to do with the code. This is a design flaw. For well designed tables and form, you should not be able to enter child records without entering the parent information first. Perhaps your subform isn't linked properly or you didn't give a precise description of the problem.

Previously, whenever i move to the subform from the mainform after editing data, there would be a prompt. In the new code, this only happens when i go back to the saved record( i know it happens because its a old record so the new record if statement does not apply), but both these methods still have prompts when clicking the button.
Again, if you were in a new record and you move between forms or try to save it will NOT prompt you.

You need to understand the code and when it fires.
 
Hm...but i have establish the relationships in the relationships windows. Perhaps i shall attach my database in this post and if possible could you take a look and see if my relationships are properly connected? Also, i noticed that when i deleted a record, it didnt cascade and subform records are not automatically deleted but i remembered selecting that option during the implementation of my relationsip...i guess my design is wrong.

Also, please post your solution here as my office does not allow for external files to be opened.
 

Attachments

Last edited:
Of course if you were editting a record and then pressed the New Record button it will prompt you because it has not yet gotten into a new record. If you were not editting a record and you press the New Record button it will not prompt you.

Nothing to do with the code. This is a design flaw. For well designed tables and form, you should not be able to enter child records without entering the parent information first. Perhaps your subform isn't linked properly or you didn't give a precise description of the problem.

Again, if you were in a new record and you move between forms or try to save it will NOT prompt you.

You need to understand the code and when it fires.

Ok then can you teach me to prompt them if they move forms or try to save it while not prompting when they press the add record button?
 
The delete works fine. I just deleted a record in main table and it cascaded.

With regards it allowing you to enter records in the subform without entering data in the main form, are you talking about I think I see what you mean. That is absolutely valid because the Foreign Key field is Null and you have not set the Required property for that field. This link explains more:

http://allenbrowne.com/ser-64.html

Regarding your last question, this is still a misunderstanding on your part. It will not prompt if it's a new record. This is the code you wrote. In your subform edit an old record, leave one box empty and press the Add New Record button. See? It prompts you!
 

Users who are viewing this thread

Back
Top Bottom