Ac said:
I have a form which user can use it to update the table and there is a
button on it named Save. I would like user to click on Save button to save
the record; otherwise, the input data will not be added into the table
after
the form closed. How can I write the code? Thanks!
Many, many things can trigger the saving of the record other than your Save
button, e.g. Shift+Enter, Save on Records, menu, closing the form, closing
Access, applying a filter, changing the sort order, navigating to another
record, tabbing past the last control, and so on.
You must learn to work with the built-in events. Access fires the
BeforeUpdate event of the form just before the record is saved. If you need
to test anything, use Form_BeforeUpdate to do that. If you need to block the
save, cancel the event.
If it is absolutely essential that the save happens only through your
button, create a module-level boolean variable in the General Declaraions:
Dim mbAllowSave As Boolean
In your button's code:
If Me.Dirty Then
mbAllowSave = True
Me.Dirty = False
mbAllowSave = False
End If
In Form_BeforeUpdate:
If Not mbAllowSave Then
Cancel = True
MsgBox "You didn't click my Save button!"
End If
mbAllowSave = False
But I wouldn't buy your app if you did that.