Forms Save

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Have added Form Save Button through add button option in form and then
selected save form option.
Now suppose i have filled half of the form and if i cancel form by pressing
X and close it, without pressing save button, the form closes but half of
data is present. Is there way I cant close form unless i press save button or
I can save partial data unless i press save button
 
There are myriads of ways that the record in the form can get saved, such as
applying a filter, changing the sort order, closing the form, moving to
another record, pressing Shift+Enter, through the Records menu, closing
Access itself, and so on.

The only way to catch them all is to use the BeforeUpdate event of the form.

1. Declare a form-level variable.
In the General Declarations section of your form's module (top, with the
Option statements):
Dim mbAllowSave As Boolean

2. In the Click event of your button, set the variable to True before the
save, and reset it afterwards:
Private Sub cmdSave_Click()
mbAllowSave = True
RunCommand acCmdSaveRecord
mbAllowSave = False
End Sub

3. Cancel Form_BeforeUpdate if the variable is not True, because the button
was not clicked:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If mbAllowSave Then
mbAllowSave = False 'reset it.
Else
Cancel = True
MsgBox "Use the Save button."
End If
End Sub

When you close the form while it is dirty, Access still asks whether to
continue (and lose the edits) or stay editing. While it is possible to trap
that in the Error event of the form, there's probably not a better
alternative. The user does need to know the edit will be lost, and if you
make it too hard for them to get out they'll corrupt your database by
justing turn off without exiting properly.
 
Back
Top