Save & new button (1 Viewer)

Ravi Kumar

Registered User.
Local time
Today, 16:57
Joined
Aug 22, 2019
Messages
162
I've written a small code to save & new button , but unfortunately it is not working. Someone please help me.
#Private Sub cmdsavenew_Click()
If Me.Dirty = True Then
If MsgBox("Do you want to save the changes for this record?", _
vbYesNo + vbQuestion, "Save Changes?") = vbNo Then
Me.Undo
Else
Me.Dirty = False
DoCmd.Save acForm, "2019"
DoCmd.OpenForm ("2019"), acNormal
End If
End If
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:27
Joined
May 7, 2009
Messages
19,169
what is not working?
Code:
Private Sub cmdsavenew_Click()
If Me.Dirty = True Then
    If MsgBox("Do you want to save the changes for this record?", _
              vbYesNo + vbQuestion, "Save Changes?") = vbNo Then
        Me.Undo
    Else
        Me.Dirty = False
        DoCmd.GoToRecord ,,acNewRec
    End If
End If
End Sub
 

Ravi Kumar

Registered User.
Local time
Today, 16:57
Joined
Aug 22, 2019
Messages
162
it is not showing new form after saving .& also I think it works only if the changes are made to the form ,but I need to open a new form even when no changes are made to the form.
Other wise I have to keep another new button ,which I don't want to.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:27
Joined
Feb 19, 2002
Messages
42,970
Access is a RAD (Rapid Application Development) tool and it helps you. One of the things it does is to save a dirty record whenever it thinks it is necessary. That means that your record will get saved whether you press the save button or not. If you actually want control of this and want to only save after the user answers a question (I wouldn't do this. The users will find it annoying and simply ignore your messages), you need to use the appropriate event to ask the question.

The save button should save the record but the question should be asked in the form's BeforeUpdate event. That way, regardless of what prompted the record save, the prompt will happen.

In the click event:
Code:
If Me.Dirty Then
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.GoToRecord ,,acNewRec
End If

In the FORM's BeforeUpdate event.
Code:
    If MsgBox("Do you want to save the changes for this record?", _
              vbYesNo + vbQuestion, "Save Changes?") = vbNo Then
        Me.Undo
        Cancel = True       
    End If

Learning how the form events work and what they are used for will help you to control when and if a record gets saved.
 

Ravi Kumar

Registered User.
Local time
Today, 16:57
Joined
Aug 22, 2019
Messages
162
dear sir , sorry to say but still the problem is unresolved,
actually what I want is :

* Even if no changes are made to existing form(record),& if I click on save & new It should open a new form.
* Suppose if I did any changes to the existing record or enter a new record it should show a message box asking whether to save or not , if I click yes it should save & got to new record.
* If I click no it should do nothing.
hope this helps to solve my issue.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:27
Joined
Feb 19, 2002
Messages
42,970
Then put the GoToRecord outside of the If.
If Me.Dirty Then
DoCmd.RunCommand acCmdSaveRecord
End If
DoCmd.GoToRecord ,,acNewRec

1. I strongly recommend that you not prompt if the user clicks the save button. He is taking a specific action by pressing a button, it is insulting to ask him if he actually meant to do that. If you want to prompt if something causes the record to be saved but the user did NOT click the save button, that makes a little more sense but the code to do that is a little more complicated.
2. Learning the "Access way" will save you from fighting with Access to bend it to your will. If you want complete control over everything, use C. If you want to use a RAD tool to make development quicker and easier, then Access is a great tool. Learn how Access works and try to go with the flow.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:27
Joined
May 7, 2009
Messages
19,169
Code:
Private Sub cmdsavenew_Click()
If Me.Dirty = True Then
    If MsgBox("Do you want to save the changes for this record?", _
              vbYesNo + vbQuestion, "Save Changes?") = vbNo Then
        Me.Undo
    Else
        Me.Dirty = False
        DoCmd.GoToRecord ,,acNewRec
    End If
Else
    Docmd.GotoRecord , , acNewRec
End If
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:27
Joined
May 7, 2009
Messages
19,169
you're welcome.
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:27
Joined
Sep 21, 2011
Messages
14,038
If you changed your thought processes and made the button create the New Record, you would get what you want.?
If the record was dirty, you would get your prompt, if not, you would still get your new record.

I would be coding as Pat recommended. Any time I see that I have duplicated code in various paths, I know there is better logic to be used.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 06:27
Joined
Feb 28, 2001
Messages
26,996
By the way, and she is usually pretty cool about this so don't worry about the error, but ... Pat Hartman isn't a "Sir" she's a "Ma'am."
 

Users who are viewing this thread

Top Bottom