disable / lock fields on form & subform based on checkbox (1 Viewer)

fibayne

Registered User.
Local time
Today, 18:35
Joined
Feb 6, 2005
Messages
236
Hi trying to lock records on a form and subform after a checkbox has been ticked, have used the code below from a previous post.

Private Sub Form_Current()

If Locked = -1 Then
Me.AllowEdits = False
Me.AllowAdditions = False
Me.AllowDeletions = False
Else
Me.AllowEdits = True
Me.AllowAdditions = True
Me.AllowDeletions = True
End If
End Sub

this is locking the Main form records is there a way to code this so that the fields on the subform are also locked when the checkbox is ticked,,,,? any help greatly appreciated,,,thanks Fi
 

fibayne

Registered User.
Local time
Today, 18:35
Joined
Feb 6, 2005
Messages
236
Hi pbaldy...many thanks for your help the code on the subform is now

Private Sub Form_Current()

If Forms!VoucherMainAdd!Locked = -1 Then
Me.AllowEdits = False
Me.AllowAdditions = False
Me.AllowDeletions = False
Else
Me.AllowEdits = True
Me.AllowAdditions = True
Me.AllowDeletions = True
End If
End Sub

many thanks Fi
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 09:35
Joined
Aug 30, 2003
Messages
36,124
No problem, though I had in mind to do it all from the main form. Whatever works is best though.
 

fibayne

Registered User.
Local time
Today, 18:35
Joined
Feb 6, 2005
Messages
236
If you wouldn't mind letting me know how to do that ? otherwise thanks again for your help Fi
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 09:35
Joined
Aug 30, 2003
Messages
36,124
Adapted from the link

Me!SubformName.Form.AllowEdits = True
 

nanscombe

Registered User.
Local time
Today, 17:35
Joined
Nov 12, 2011
Messages
1,082
Since the value of the properties is always the opposite of the value of the Locked control you could probably tidy the code up a little ...

Code:
Private Sub Form_Current()
Dim blAllowChanges as Boolean

  ' Assume a default of True for the checkbox "Locked" if it is Null
  blAllowChanges = [b]NOT[/b] nz(Me.Locked, True)
 
  Me!SubformName.Form.AllowEdits = blAllowChanges
  Me!SubformName.Form.AllowAdditions = blAllowChanges
  Me.AllowDeletions = blAllowChanges

End Sub
 
Last edited:

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 02:35
Joined
Jan 20, 2009
Messages
12,851
My general coding preference is to use more structure. Although it take a tiny bit little longer to set up, it makes the code so much easier to use and maintain.

Add a Public Property to the subform object.
Code:
Public Property Let Locked(Lock As Boolean)
 
    With Me
        .AllowEdits = Not Lock
        .AllowAdditions = Not Lock
        .AllowDeletions = Not Lock
    End With
 
End Property


This way the subform can be enabled or disabled with a single line from anywhere in the project. Moreover, no matter how that form is used, the Locked Property is always available.


(Some developers might criticise the use of Locked because it is already a Property of Controls. However its context is clear and since forms do not have a native Locked property it is fine to use it.)

On the main form the whole locking process can be put into a Sub.

Code:
Private Sub LockForm(Lock As Boolean)
 
    With Me
        .AllowEdits = Not Lock
        .AllowAdditions = Not Lock
        .AllowDeletions = Not Lock
        .subformcontrolname.Form.Locked = Lock
    End With
 
End Sub

This simplifies the call to a single command.

Code:
Private Sub Form_Current()
 
    LockForm Me.chkLock
 
End Sub

This makes the action very clear without the distraction of several lines of code whenever it is used.

Alternatively it can be constructed as a Property too. (I have used one each way to demonstrate the syntax.)
 

Users who are viewing this thread

Top Bottom