Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 09-18-2019, 08:36 PM   #1
Ravi Kumar
Newly Registered User
 
Join Date: Aug 2019
Posts: 86
Thanks: 106
Thanked 0 Times in 0 Posts
Ravi Kumar is on a distinguished road
Save & new button

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

Ravi Kumar is offline   Reply With Quote
Old 09-18-2019, 09:20 PM   #2
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,584
Thanks: 68
Thanked 2,755 Times in 2,639 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Save & new button

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
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
Ravi Kumar (09-18-2019)
Old 09-18-2019, 09:31 PM   #3
Ravi Kumar
Newly Registered User
 
Join Date: Aug 2019
Posts: 86
Thanks: 106
Thanked 0 Times in 0 Posts
Ravi Kumar is on a distinguished road
Re: Save & new button

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.

Ravi Kumar is offline   Reply With Quote
Old 09-18-2019, 09:54 PM   #4
Pat Hartman
Super Moderator
 
Join Date: Feb 2002
Location: Stratford,Ct USA
Posts: 28,402
Thanks: 15
Thanked 1,625 Times in 1,543 Posts
Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all
Re: Save & new button

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.
__________________
Bridge Players Still Know All the Tricks
Pat Hartman is offline   Reply With Quote
The Following User Says Thank You to Pat Hartman For This Useful Post:
Ravi Kumar (09-18-2019)
Old 09-18-2019, 10:07 PM   #5
Ravi Kumar
Newly Registered User
 
Join Date: Aug 2019
Posts: 86
Thanks: 106
Thanked 0 Times in 0 Posts
Ravi Kumar is on a distinguished road
Re: Save & new button

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.
Ravi Kumar is offline   Reply With Quote
Old 09-18-2019, 10:15 PM   #6
Pat Hartman
Super Moderator
 
Join Date: Feb 2002
Location: Stratford,Ct USA
Posts: 28,402
Thanks: 15
Thanked 1,625 Times in 1,543 Posts
Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all Pat Hartman is a name known to all
Re: Save & new button

Then put the GoToRecord outside of the If.
Quote:
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.
__________________
Bridge Players Still Know All the Tricks
Pat Hartman is offline   Reply With Quote
Old 09-18-2019, 10:27 PM   #7
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,584
Thanks: 68
Thanked 2,755 Times in 2,639 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Save & new button

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

__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
Ravi Kumar (09-18-2019)
Old 09-18-2019, 10:28 PM   #8
Ravi Kumar
Newly Registered User
 
Join Date: Aug 2019
Posts: 86
Thanks: 106
Thanked 0 Times in 0 Posts
Ravi Kumar is on a distinguished road
Re: Save & new button

Thank you so much it worked!!!
Ravi Kumar is offline   Reply With Quote
Old 09-18-2019, 10:30 PM   #9
arnelgp
error reading drive A:
 
arnelgp's Avatar
 
Join Date: May 2009
Location: somewhere out there
Posts: 8,584
Thanks: 68
Thanked 2,755 Times in 2,639 Posts
arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice arnelgp is just really nice
Re: Save & new button

you're welcome.
__________________
"Never stop learning, because life never stops teaching"
arnelgp is offline   Reply With Quote
The Following User Says Thank You to arnelgp For This Useful Post:
Ravi Kumar (09-23-2019)
Old 09-18-2019, 10:44 PM   #10
Gasman
Enthusiastic Amateur
 
Gasman's Avatar
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 4,814
Thanks: 457
Thanked 908 Times in 872 Posts
Gasman is a jewel in the rough Gasman is a jewel in the rough Gasman is a jewel in the rough
Re: Save & new button

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.
__________________
Access novice. Sometimes trying to give something back.
Access 2007

Please, please use code tag # when posting code snippets

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Gasman is offline   Reply With Quote
The Following User Says Thank You to Gasman For This Useful Post:
Ravi Kumar (09-23-2019)
Old 09-19-2019, 05:34 AM   #11
The_Doc_Man
Happy Retired Curmudgeon
 
Join Date: Feb 2001
Location: Suburban New Orleans, LA, USA
Posts: 15,107
Thanks: 104
Thanked 1,897 Times in 1,731 Posts
The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold The_Doc_Man is a splendid one to behold
Re: Save & new button

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."

__________________
I'm a certified grandpa (3 times now) and proud of it.
Retired over one year and survived being home all day with the wife. She must really love me.
If I have helped you, please either click the thanks or click the scales.
The_Doc_Man is offline   Reply With Quote
The Following User Says Thank You to The_Doc_Man For This Useful Post:
Ravi Kumar (09-23-2019)
Reply

Tags
beginner user , save and new , vba

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question Need VB coding for access data base to save the value in text box using save button JAWAJANA General 6 12-19-2017 09:46 PM
[SOLVED] Unable to navigate between records, custom save button doesn't save Emjay Forms 4 07-24-2013 07:53 PM
Only save record in subform when a save button is pressed patkeaveney Forms 3 02-05-2008 03:30 PM
Save Button versus Save safety net monkeytunes Modules & VBA 8 09-01-2004 12:17 PM
Combine custom save button on subform with save button on main form popeye Forms 2 08-27-2004 07:58 AM




All times are GMT -8. The time now is 10:43 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World