how to unlock one field on a locked subform (1 Viewer)

Local time
Today, 06:47
Joined
Apr 25, 2019
Messages
62
I have a main form that locks the one subform after 12 hours. I need to be able to keep just one field on the subform unlocked at all times. There are too many fields on the subform to lock and unlock each one indivdually. Right now I just lock the whole subform and unlock the whole subform with a single vba code. But i cant seem to unlock the single field on the subform, once the whole subform is locked. any ideas?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 07:47
Joined
May 21, 2018
Messages
8,529
Simply select all the controls except the one in design view then tag them "Lock"
Now instead of locking the form run this code.
Code:
Public Sub lockControls()
  Dim ctrl As Access.Control
  For Each ctrl In Me.Controls
    If ctrl.Tag = "Lock" Then
      ctrl.Locked = True
    End If
Next ctrl
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:47
Joined
Feb 19, 2002
Messages
43,275
This code is similar to what Maj posted but it works the opposite way. You only have to tag controls that you want to handle differently. Doing it this way prevents accidents when you add new controls and don't remember to set their tag correctly. You always know if you want the field to be treated differently so you are much more conscious of how to make that happen. It also handles different types of controls.

This code is intended to be used by ANY form so you would place it in a standard module. Then you call it in the form's current event.

Call LockControls(Me, True) '''' to lock
Call LockControls(Me, False) '''' to unlock

It doesn't matter whether the form is a main form or a subform, the Me reference always points to the form that called the procedure.

So, for controls, that you want to always remain Locked no matter what the setting is, you set their tag property to Lock. For controls that you always want to remain unlocked no matter what the setting is, you set the tag property to Unlock.

So, for you, no matter how many controls you have on the form in question, using this procedure, you only need to set the tag property fr the special field and you would set it to unlock.

In the Current event, you would normally want existing records to be locked but new records to be unlocked. You may also need a button on the form that toggles the form to unlock.

You may also want to call the procedure from the AfterUpdate event of the form to lock the controls atain.


Code:
Public Sub LockControls(frm As Form, bLock As Boolean)
    Dim ctl As Control
    For Each ctl In frm.Controls
    Select Case ctl.ControlType
        Case acTextBox, acComboBox, acListBox, acCheckBox
            Select Case ctl.Tag
                Case "NoLock"
                    ctl.Locked = False
                Case "Lock"
                    ctl.Locked = True
                Case Else
                    ctl.Locked = bLock         'toggle locks
            End Select
        Case acOptionGroup, acOptionButton, acCheckBox      ''not working
             Select Case ctl.Tag
                Case "NoLock"
                    ctl.Locked = False
                Case "Lock"
                    ctl.Locked = True
                Case Else
                    ctl.Locked = bLock         'toggle locks
            End Select
        Case acCommandButton
            Select Case ctl.Tag
                Case "NoLock"
                    ctl.Enabled = True
                Case "Lock"
                    ctl.Enabled = False
                Case Else
                    ctl.Enabled = Not bLock         'toggle locks
            End Select
    End Select
Next ctl
Set ctl = Nothing
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:47
Joined
May 7, 2009
Messages
19,244
you can also Keep your subform Locked set to Yes.
then on design view of your form/subform, click on the Textbox on the subform and add this to the following events:
lock.png


Now, create LockMe function on the subform.
you substitute your Main form name to MainFormNameHere and your subform name to SubformNameHere

Code:
Public Function LockMe(ByVal tfLock As Boolean)
        Forms!MainFormNameHere!SubformNameHere.Locked = tfLock
End Function

see this demo. the textbox "Last Name" on the subform is Unlocked.
 

Attachments

  • LockUnlock.accdb
    448 KB · Views: 102

Gasman

Enthusiastic Amateur
Local time
Today, 12:47
Joined
Sep 21, 2011
Messages
14,305
you can also Keep your subform Locked set to Yes.
then on design view of your form/subform, click on the Textbox on the subform and add this to the following events:
View attachment 104840

Now, create LockMe function on the subform.
you substitute your Main form name to MainFormNameHere and your subform name to SubformNameHere

Code:
Public Function LockMe(ByVal tfLock As Boolean)
        Forms!MainFormNameHere!SubformNameHere.Locked = tfLock
End Function

see this demo. the textbox "Last Name" on the subform is Unlocked.
OK, I am confused by that? :(
Surely if you are unlocking when the control gets the focus/clicked, then you may as well leave it unlocked?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:47
Joined
May 7, 2009
Messages
19,244
then you may as well leave it unlocked?
you can't Selectively unlocked a control when the Form itself is locked.

it is actually Unlocking the Form when the "last name" get focus.
and Lock it again when the focus shift to another control.
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:47
Joined
Sep 21, 2011
Messages
14,305
you can't Selectively unlocked a control when the Form itself is locked.

it is actually Unlocking the Form when the "last name" get focus.
and Lock it again when the focus shift to another control.
Yes, I realise that, but surely just locking all controls after 12 hours, simple loop?, then unlock that one control, is a one time operation?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:47
Joined
May 7, 2009
Messages
19,244
locking all controls after 12 hours, simple loop?,
you adapt whatever will make you happy.
i am not forcing anything to you.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 12:47
Joined
Sep 12, 2006
Messages
15,656
The other things is don't worry about setting a few controls causing delays. It's infinitesimally quick. Not quite that quick, but you are talking a handful of instructions out of potentially MIPS, so it's as good as instant. It's writing and managing it that takes time, but you can't really avoid that to get the right user experience, and you should only need to do the set up once.
 

Users who are viewing this thread

Top Bottom