Button for editing / lock editing data in Access 2016

Viewed 45

I am blocking users from accidentally editing datasets. They have to hit a button to edit stuff. This button sets the "AllowEdits" setting of the form to "true" which is "false" by default. It also changes the caption of the button accordingly.

My problem is: the button does not bring back the lock, when I hit it a second time. Of course I have the same code reversed for this case.

What am I missing?

Private Sub toggleEdit_Click()

If Me.AllowEdits = True Then
    Me.AllowEdits = False
    Me.toggleEdit.Caption = "Unlock"
Else
    Me.AllowEdits = True
    Me.toggleEdit.Caption = "Lock"
End If

End Sub
1 Answers

As Kostas K. supposed: saving the record before locking it does the trick. You have to save the record before you can lock editing again.

So the working code is this:

Private Sub toggleEdit_Click()

If Me.AllowEdits = True Then
    DoCmd.RunCommand acCmdSaveRecord
    Me.AllowEdits = False
    Me.toggleEdit.Caption = "Unlock"
Else
    Me.AllowEdits = True
    Me.toggleEdit.Caption = "Lock"
End If

End Sub
Related