Full Version: locking all controls on a form
UtterAccess Forums > Microsoft® Access > Access Forms
Nyteshade
I have a form that is displays two kinds of application defined records: 1) activated and 2) deactivated. If the form displays an activated record then edits are allowed to the controls or user may delete the record. If the form displays a deactivated record then no edits should be allowed to any controls and only delete of the record is allowed. So, rather than use a 'with' deactivated record lock and disabled 30 controls I'm hoping there is a method to lock and disable all normally edited controls and then simply enabled the delete command button? Thx all!
oCmd.OpenForm "frmStoreVendorUnitAddEdit", acNormal
If Me.unitIsActive.Value = -1 Then
With Forms![frmStoreVendorUnitAddEdit]
.Caption = "Store Storage Unit Edit"
.lblFormTitle.Caption = "Edit Offsite Storage"
.txtCurrUnitRec = intCurrRec
.FrameType.SetFocus
.txtunitID.BackColor = 12632256
.txtunitID.Locked = True
.txtunitID.Enabled = False
.cmdDelete.Enabled = True
.cmdsave.Caption = "Save"
.DataEntry = False
.FilterOn = True
.Filter = "[unitvenID] = " & intVendor & " and [unitID] = " & strUnit & " and [unitStore] = " & intStore
.unitVenID.Value = [Forms]![frmOSSISMain]![frmStoreVendor].[Form]![venID]
.unitStore.Value = [Forms]![frmOSSISMain]![Shop]
End With
Else
With Forms![frmStoreVendorUnitAddEdit]
'GENERIC LOCK & DISABLE ALL CONTROLS - GREY OUT THE CONTROL BACKGROUND
.cmdDelete.Enabled = True
End With
End If
JayNoelOlimpo
Hi;
se ...
CODE
Dim ctrl As Control
For Each ctrl In Me.Controls
    If ctrl.Tag = "Lockme" And Me.txtActivated = True Then
        ctrl.Enabled = False
    ElseIf ctrl.Tag = "Lockme" And Me.txtActivated = False Then
        ctrl.Enabled = True
    End If
Next ctrl

HTH.
Doug Steele
Two approaches off the top of my head.
One is to store something in the Tag property of those controls that you want to be locked, then loop through all of the controls, locking those that have their Tag property set to that value:
CODE
Sub ToggleLocking(WhatForm As Form, LockStatus As Boolean)
Dim ctlCurr As Control
  
  For Each ctlCurr In WhatForm.Controls
    If ctlCurr.Tag = "LockMe" Then
      ctlCurr.Locked = LockStatus
    End If
  Next ctlCurr
  
End Sub

To lock the given controls, you'd use:
Call ToggleLocking(Me, True)
To unlock them, you'd use:
Call ToggleLocking(Me, False)
The other approach would also involve looping through all the controls, and determine which control types can be locked (for example, Labels can't):
CODE
Sub ToggleLocking(WhatForm As Form, LockStatus As Boolean)
Dim ctlCurr As Control
  
  For Each ctlCurr In WhatForm.Controls
    Select Case ctlCurr.ControlType
      Case acCommandButton, acOptionButton, acCheckBox, acOptionGroup, acBoundObjectFrame, _
        acTextBox, acListBox, acComboBox, acSubform, acObjectFrame, acCustomControl, acToggleButton
        ctlCurr.Locked = LockStatus
    End Select
  Next ctlCurr
  
End Sub
Nyteshade
Thx Joel and Doug for replying so early, this will be my first encounter with the Tag property. I'll need to read up on it and give it a try; and thx for the examples!
Doug Steele
You might also check my June, 2004 "Access Answers" column in Pinnacle Publication's "Smart Access".
You can download the column (and sample database) for free at Smart Access columns
Nyteshade
I've started with your example and here's what happens with this code:
im ctrl as Control ' I NOTICED THAT THE WORD 'Control' does not turn blue but remains black? All my other Dim declarations have 'as' as black and anything that follows turns blue! FYI: The type 'Control' does show up in the object list right after I type: 'Dim ctrl as'.
MsgBox "Line B4 For Statement"
For Each ctrl In Me.Controls
MsgBox "Within For Statement"
MsgBox ctrl ' THIS LINE DISPLAYS: " 1 of 9 " WHAT DOES THIS MEAN? I HAVE ABOUT 30 CONTROLS USING TAG!?
If ctrl.Tag = "DeactivatedFlag" Then
MsgBox "Within If Statement"
ctrl.Enabled = False ' THIS LINE BLOWS UP CITING: Object Required
ctrl.Locked = True
ctrl.BackColor = 12632256
End If
Next ctrl
Thx fellas!
Nyteshade
Not all controls have a Back Color property so the loop continues to set the 'enabled' and 'locked' properties but fails to set the Back Color property on all controls once the first error is encountered, interesting.
For Each ctrl In [Forms]![frmStoreVendorUnitAddEdit].Controls
MsgBox [ctrl].[Name]
MsgBox [ctrl].[Tag]
If ctrl.Tag <> "" Then
If ctrl.Tag = "DeactivatedFlag" Then
ctrl.Enabled = False
ctrl.Locked = True
ctrl.BackColor = 12632256 'AFTER MUCH HEAD SCRATCHING ELIMINATIED THIS LINE
End If
End If
Next ctrl
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.