How to prevent F5 key from saving data from userform in access

淺唱寂寞╮ 提交于 2019-12-11 11:52:25

问题


I have a bound form in MS Access and I use a submit button to insert the data from form to table. I have around 10 fields in form and even if I fill only 1 field and press the F5 button, it saves the data with just one field. How to stop F5 key from doing this.

Edit - also when I close the form with partially filled data or if it gets closed accidently or when if i open design mode from there, it collects that partially filled data and then creates an entry, how to stop userform making entries via other means and make it only create record on button click.


回答1:


In my opinion, you should not stop the F5 button from doing this, you should stop anything else than your submit button from saving data.

This can be achieved with some VBA code:

The save button is named cmdSave in this example

Private saveButtonPressed As Boolean

Private Sub cmdSave_Click()
    saveButtonPressed = True
    DoCmd.RunCommand acCmdSaveRecord
End Sub


Private Sub Form_BeforeInsert(Cancel As Integer)
    If Not saveButtonPressed Then
        'Update through other means
        Cancel = True
    End If
    saveButtonPressed = False
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
    If Not saveButtonPressed Then
        'Update through other means
        Cancel = True
    End If
    saveButtonPressed = False
End Sub


来源:https://stackoverflow.com/questions/47682667/how-to-prevent-f5-key-from-saving-data-from-userform-in-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!