2

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
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73

1 Answers1

1

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
  • Using `Me.Dirty = False` is considered the safer and more reliable alternative. Seen good explanations from Allen Browne and @Andre https://stackoverflow.com/a/51378643/2419128 – dbmitch May 21 '22 at 17:00