-->

MS Access: How to disable Form autosave and create

2019-07-04 01:13发布

问题:

I understand that inputting data in a form in MS Access automatically updates the fields in the table. However, how do I disable this feature and instead allow a user to click a "Save" button at the end to update the records? Have been reading online that I need VBA etc. that I have no experience with.

回答1:

This can only be done in code.

You need to set a module level boolean variable to control saving (auto vs manual) and set its value to True when the save button is clicked.

Private mIsUserUpdate As Boolean 'Flag

'Cancel Auto Save
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not mIsUserUpdate Then Cancel = True
End Sub

'Manual Save
Private Sub YourButtonName_Click()

    '...
    'Do work
    '...

    mIsUserUpdate = True 'OK to save
    DoCmd.RunCommand acCmdSaveRecord
    mIsUserUpdate = False 'Revert
End Sub


回答2:

Unfortunately you cannot disable autosaving in forms. As a workaround you can copy data to temporary table, allow user to edit the data as needed and by clicking "Save" button copy changed data back to main table.

Also as a workaround can be used canceling record saving as it described in the answer of @Kostas K. within one form, but in this case you won't be able to change edited record or use any controls outside of edit form if it is a subform, so Save button should be located on the same form as edited data.



回答3:

Another way to do this would be to use a pair of text boxes. One is bound to the database and the other is not. The bound text box would not be visible.

Text1 is not bound
Text2 is bound

Code for the save button..

Me.Text2.value = me.Text1.value
if me.dirty=true then me.dirty=false

You can get into specific conditions...

If isnull(me.Text1.value) then me.Text2=.value=""