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

what is the save command code in ms access?

P: 3
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 :)
4 Days Ago #1
Share this Question
Share on Google+
5 Replies

Expert Mod 15k+
P: 30,736
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
4 Days Ago #2

P: 3
thank you sir NeoPa i will try it sir.. ill post here if it works
thanks a lot :D
4 Days Ago #3

P: 3
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
  5. End Sub
  6. End Sub
thats where the error is ... i think i write it wrong in the code
4 Days Ago #4

Expert Mod 100+
P: 2,297

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.
3 Days Ago #5

Expert Mod 15k+
P: 30,736
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?
3 Days Ago #6

Post your reply

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