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?
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
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
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:
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.
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.
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.