423,491 Members | 2,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,491 IT Pros & Developers. It's quick & easy.

what is the save command code in ms access?

P: 5
hello guys im a beginner in programming .. i have a problem in saving option in access... in access when you type information in fields it automatically save in database if you click the next button command even if you are not clicking the save button..
and i want is, the data will not be save if the save button command is not using..

(sorry for the grammar english is not my first language)

hope you understand my point .. thank you :)
Jun 14 '18 #1

✓ answered by NeoPa

Hi Emmannuelle.

There are two parts to this question :
  1. What code is used to save a record?
  2. How do I ensure it only saves when I use the save code, and not simply when the operator moves to another record?

For #1 I have a routine as the code itself is such that it doesn't look like it's going to save the record.
Expand|Select|Wrap|Line Numbers
  1. 'SaveRec() Saves the current record on frmMe.
  2. Public Sub SaveRec(frmMe As Form)
  3.     'No error handling here.  Should be handled by calling code if required.
  4.     'This code is a little weird but how saving has been implemented in Access.
  5.     frmMe.Dirty = False
  6. End Sub
This is part of a standard module that can be called by the code from any Form.

For #2 we use a variable that is only set to allow saves from the part of our code where we want the saves to succeed. The variable is set as Private in the Form's code module. In the BeforeUpdate() Event handler code you use the Cancel parameter if that variable isn't set, so the save fails.
Expand|Select|Wrap|Line Numbers
  1. Private blnAllowSave As Boolean
  2. ...
  3. Private Sub Form_BeforeUpdate(Cancel As Integer)
  4.     If Not blnAllowSave Then
  5.         Cancel = True
  6.         Call MsgBox(Prompt:="Please use the Save Button to save your changes." _
  7.                   , Buttons:=VbOKOnly Or VbInformation _
  8.                   , Title:=Me.Name)
  9.     End If
  10. End Sub
  11. ...
  12. Private Sub cmdSave_Click()
  13.     blnAllowSave = True
  14.     Call SaveRec(frmMe:=Me)
  15.     blnAllowSave = False
  16. End Sub

Share this Question
Share on Google+
9 Replies


NeoPa
Expert Mod 15k+
P: 31,037
Hi Emmannuelle.

There are two parts to this question :
  1. What code is used to save a record?
  2. How do I ensure it only saves when I use the save code, and not simply when the operator moves to another record?

For #1 I have a routine as the code itself is such that it doesn't look like it's going to save the record.
Expand|Select|Wrap|Line Numbers
  1. 'SaveRec() Saves the current record on frmMe.
  2. Public Sub SaveRec(frmMe As Form)
  3.     'No error handling here.  Should be handled by calling code if required.
  4.     'This code is a little weird but how saving has been implemented in Access.
  5.     frmMe.Dirty = False
  6. End Sub
This is part of a standard module that can be called by the code from any Form.

For #2 we use a variable that is only set to allow saves from the part of our code where we want the saves to succeed. The variable is set as Private in the Form's code module. In the BeforeUpdate() Event handler code you use the Cancel parameter if that variable isn't set, so the save fails.
Expand|Select|Wrap|Line Numbers
  1. Private blnAllowSave As Boolean
  2. ...
  3. Private Sub Form_BeforeUpdate(Cancel As Integer)
  4.     If Not blnAllowSave Then
  5.         Cancel = True
  6.         Call MsgBox(Prompt:="Please use the Save Button to save your changes." _
  7.                   , Buttons:=VbOKOnly Or VbInformation _
  8.                   , Title:=Me.Name)
  9.     End If
  10. End Sub
  11. ...
  12. Private Sub cmdSave_Click()
  13.     blnAllowSave = True
  14.     Call SaveRec(frmMe:=Me)
  15.     blnAllowSave = False
  16. End Sub
Jun 14 '18 #2

P: 5
thank you sir NeoPa i will try it sir.. ill post here if it works
thanks a lot :D
Jun 14 '18 #3

P: 5
sir NeoPa your 2nd code really did work .. but im having a problem in the save code...


Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdsave_Click()
  2.   Public Sub SaveRec (frmMe As Form)
  3. frmMe.Dirty = False
  4.  
  5. End Sub
  6. End Sub
  7.  
  8.  
  9.  
thats where the error is ... i think i write it wrong in the code
Jun 14 '18 #4

twinnyfo
Expert Mod 2.5K+
P: 2,608
emmannuelle15,

First, you should do a direct copy and paste of the code provided by NeoPa. It is clear from your last post that this did not happen.

When you think of a procedure, think of it as a distinct unit of code. Anything between the Private Sub [NameOfProcedure]() and End Sub statements are autonomous and can't have anything inside except what belongs specifically to that procedure.

For your preivous post remove lines 1 and 6 to start. You must also have the second portion of NeoPa's code in your form's VBA module. It should work "as is", using your command button.

Hope this hepps.
Jun 14 '18 #5

NeoPa
Expert Mod 15k+
P: 31,037
Indeed. Use Copy/Paste (Recommended) or, if that's not possible because your names are different, at least Copy/Paste first, and then change only those names you need to.

If you can simply copy visually then fine, but you need to be able to do it 100% reliably if you do. Why bother though, when using Copy/Paste is both easier and more reliable?
Jun 14 '18 #6

P: 5
sir twinnyfo and sir NeoPa thank you for the advice i really appreciate it.. ill keep it in mind :D
Jun 19 '18 #7

twinnyfo
Expert Mod 2.5K+
P: 2,608
Were we able to resolve your primary issue?
Jun 19 '18 #8

P: 5
yes sir :D thank you
Jun 20 '18 #9

NeoPa
Expert Mod 15k+
P: 31,037
Good to see. This is a helpful thread as many need help with exactly that.
Jun 20 '18 #10

Post your reply

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