Lock record (1 Viewer)

Leo_Polla_Psemata

Registered User.
Local time
Today, 10:47
Joined
Mar 24, 2014
Messages
364
The below code, performs the required action,
If i click on the yes/no field 'lockrec', the record gets locked, however,
if i close and reopen the form, then form edit is allowed.

How could i make it so the record remains locked.
Code:
Private Sub LockRec_Click()

    If Me.lockrec = True Then
        Me.AllowEdits = False
    Else
        Me.AllowEdits = True
    End If

End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:47
Joined
May 7, 2009
Messages
19,247
use current event of your form:

private sub form_current()
call LockRec_Click
end sub
 

Leo_Polla_Psemata

Registered User.
Local time
Today, 10:47
Joined
Mar 24, 2014
Messages
364
use current event of your form:

private sub form_current()
call LockRec_Click
end sub
I made it.
Is there any way we could leave only "lockrec" key unlocked?
Because, once i lock the record, this protects from accidental edits, then i might need to unlock but now only on table level i could do it.
 

Gasman

Enthusiastic Amateur
Local time
Today, 18:47
Joined
Sep 21, 2011
Messages
14,339
The below code, performs the required action,
If i click on the yes/no field 'lockrec', the record gets locked, however,
if i close and reopen the form, then form edit is allowed.

How could i make it so the record remains locked.
Code:
Private Sub LockRec_Click()

    If Me.lockrec = True Then
        Me.AllowEdits = False
    Else
        Me.AllowEdits = True
    End If

End Sub
Think about it?
How is Access meant to know you want that record locked?

Either LockRec must be bound or some other field is populated, perhaps DateCompleted?
Either way, you must tell Access how it can determine whether the record should be locked or not.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:47
Joined
Feb 19, 2002
Messages
43,331
Your code relies on the user to click a button. However, the result of that button click isn't saved anywhere so therefore, when you come back to the record, how would Access know you clicked the button some time in the past. Does that clarify the problem for you? My reply is essentially the same as what you got from Gasman, just worded a little differently.

If there is some piece of data in the record that indicates whether or not it should be locked such as a ShipDT that is not null, Closed = True, etc, you can use that.

Tell us what the trigger is and we'll offer a concrete solution.

PS rather than locking a record using AllowEdits which leads to other issues, I trap the change in the Form's dirty event. This event is triggered as soon as the first character is typed in the form. In that event, I would check the appropriate data field and prevent the user from changing anything, or I would allow the change to proceed.

Code:
Private Sub Form_Dirty(Cancel As Integer)
    If IsDate(Me.ShipDT) Then
        MsgBox "This order can no longer be changed.  It has already been shipped.", vbOKOnly
        Cancel = True
        Me.Undo
        Exit Sub
    End If
End Sub
Using this method leaves the buttons active as well as the search boxes so you can look at other records if you need to.
 

Users who are viewing this thread

Top Bottom