By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

How do I Save Data on Form Only After Button Clicked

P: 1
Hi There

I have a data entry form bound to table , i want to add records only using command button in other words i want to prevent automatic saving while writing into fields .

Thanks
Jan 24 '12 #1

✓ answered by TheSmileyCoder

I disagree dsatino. There is absolutely no need to resort to unbound forms and the headeaches that come with that.


Add a private boolean variable to your form:
Expand|Select|Wrap|Line Numbers
  1. Private bSaveClicked as Boolean
Add code to your command button:
Expand|Select|Wrap|Line Numbers
  1. bSaveClicked=true
  2. On Error Resume Next
  3. Docmd.Runcommand accmdsaverecord
  4. bSaveClicked=false
Add code to your forms beforeupdate:
Expand|Select|Wrap|Line Numbers
  1. If not bSaveClicked Then
  2.   Msgbox "You are trying to navigate away from the active record. Please either save your changes, or press ESC to cancel your changes.",VbOkOnly+vbInformation
  3. Cancel=True
  4. End If

Share this Question
Share on Google+
8 Replies


dsatino
100+
P: 393
Then you don't want to bind the bind the form to the table. Just capture the values entered in a sql string and run that on the button click.
Jan 24 '12 #2

TheSmileyCoder
Expert Mod 100+
P: 2,321
I disagree dsatino. There is absolutely no need to resort to unbound forms and the headeaches that come with that.


Add a private boolean variable to your form:
Expand|Select|Wrap|Line Numbers
  1. Private bSaveClicked as Boolean
Add code to your command button:
Expand|Select|Wrap|Line Numbers
  1. bSaveClicked=true
  2. On Error Resume Next
  3. Docmd.Runcommand accmdsaverecord
  4. bSaveClicked=false
Add code to your forms beforeupdate:
Expand|Select|Wrap|Line Numbers
  1. If not bSaveClicked Then
  2.   Msgbox "You are trying to navigate away from the active record. Please either save your changes, or press ESC to cancel your changes.",VbOkOnly+vbInformation
  3. Cancel=True
  4. End If
Jan 24 '12 #3

NeoPa
Expert Mod 15k+
P: 31,494
I must agree with Smiley here (Sorry Dsatino). The SQL string approach should be avoided where possible, as it is trying to do a job for Access (badly) that Access already does well. Many people seem to see this as a way forward, but I have never seen anything but trouble come from leading people away from the natural way of using Access. It's a bit like using an Object-Oriented computer language, but writing procedural code anyway.

May I just add that this reflects no disrespect towards Dsatino, whom I know to be an accomplished Access expert in various ares. In this case though, we disagree.
Jan 24 '12 #4

dsatino
100+
P: 393
No disrespect taken. A better way is a better way... :)
Jan 24 '12 #5

NeoPa
Expert Mod 15k+
P: 31,494
A new member, Crobar, posted a request for help with Smiley's code in here so I moved it to a new thread (How Do I Use Code Posted?).
Jan 4 '15 #6

P: 1
@TheSmileyCoder
Replace "bSaveButtonClicked" with "bSaveClicked"
Jun 6 '17 #7

NeoPa
Expert Mod 15k+
P: 31,494
Good spot :-)
Jun 6 '17 #8

TheSmileyCoder
Expert Mod 100+
P: 2,321
Thank you. I've fixed the typo. I guess this falls under the category "better late than never"
Jun 13 '17 #9

Post your reply

Sign in to post your reply or Sign up for a free account.