I have a simple form. I want to add a Save Button so when a user is typing in
the feilds of a form, the data shuould not save to the table before the Save
button is press. When a user press save button, than the data shulod post in
table.
How to do that?
One way is to put a shared variable in the form's module by typing
Dim OKToClose As Boolean
in the *very top* of the module, before any of the Sub lines. If you open the
code window in form design the top of it should read
Option Explicit
Option Compare Database
Dim OKToClose As Boolean
Set OKToClose to False in the form's Current event (so as each new record is
selected or created it's set to False).
The save button's Click event should just be:
Private Sub cmdSave_Click()
OKToClose = True
If Me.Dirty Then
Me.Dirty = False
End If
End Sub
Setting the form's Dirty property to false saves the record to disk.
In the form's BeforeUpdate event put code
Private Sub Form_BeforeUpdate(Cancel as Integer)
If Not OKToClose Then
Cancel = True
MsgBox "Please use the Save button to save the record", vbOKOnly
End If
End Sub
You may want to add a Cancel button too, in case the user wants to close
without saving. Its code should simply be
Private Sub cmdCancel_Click()
Me.Undo
End Sub