Example Apps for Businesses, Schools & Developers

Version 3.0           Last Updated 29 Jan 2022                 Approx 1 MB


The attached utility demonstrates a way of setting the state of a group of controls at the same time using the controls' Tag property.

The properties that can be controlled are: Visible, Enabled, Locked

However, some control types do not allow all of the properties to be changed.
For example, labels can not be disabled or locked
For full details for each control type & its properties, see the table tblControlTypes or view its contents in the subform control on the main form.

Set the tag value on the form property sheet in design view. You can use any alphanumeric string for tag values e.g. HIDE, LOCK, B1, XXX etc.

In this example, all the form controls have tags A, B, C or D. Use the buttons to control the state of all controls with tags A, B, C

To ensure the form remains usable, I have ensured controls with tag D remain visible, enabled & unlocked at all times!
NOTE: The forms aren't intended to be elegant, just to show what can be done using this approach.


All controls enabled / visible & unlocked

Image1
Controls with tag A disabled, B hidden, C locked

Image2
Controls with tag A locked, B disabled, C hidden

Image3
Controls with tag A hidden, B locked, C disabled

Image4


Code

All the code referenced is in the module modControlState.

There are 3 procedures: ShowControls, EnableControls & LockControls

Option Compare Database
Option Explicit

Global ctrl As Control
----------------------------------------------------

Public Sub ShowControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)

On Error GoTo Err_Handler

'set controls to visible or not according to the control tag value

For Each ctrl In Screen.ActiveForm.Controls
Select Case ctrl.ControlType

Case acPageBreak

'no code here - these can't be locked
Case Else
If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.Visible = State
End Select
Next ctrl

Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " in ShowControls procedure: " & Err.Description
Resume Exit_Handler
End Sub

----------------------------------------------------

Public Sub LockControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)

On Error GoTo Err_Handler
'set controls to locked or not according to the control tag value
For Each ctrl In Screen.ActiveForm.Controls
Select Case ctrl.ControlType

Case acLabel, acCommandButton, acTabCtl, acPage, acImage, acLine, acRectangle, acPageBreak
'no code here - these can't be locked
Case Else
If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.Locked = State
End Select

Next ctrl

Exit_Handler:
Exit Sub
Err_Handler:
MsgBox "Error " & Err.Number & " in LockControls procedure: " & Err.Description
Resume Exit_Handler

End Sub
----------------------------------------------------

Public Sub EnableControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)

On Error GoTo Err_Handler


'set controls to locked or not according to the control tag value
For Each ctrl In Screen.ActiveForm.Controls
Select Case ctrl.ControlType

Case acLabel, acImage, acLine, acRectangle, acPageBreak

'no code here - these can't be disabled
Case Else
If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.Enabled = State
End Select

Next ctrl

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox "Error " & Err.Number & " in EnableControls procedure: " & Err.Description
Resume Exit_Handler


End Sub


The example app contains 2 forms - identical apart from colour
This is just to confirm that the forms are controlled independently as you would expect!
In addition, one form demonstrates the use of automatic form resizing code for different screen resolutions; the other doesn't.
For more information on automatic form resizing, see my three part article: Automatic Form Resizing Tutorial

To use this code in your own apps, copy the module modControlState to your own project and use code similar to that below:

Private Sub Form_Load()

'set all controls visible, enabled and unlocked when form loads
EnableControls True, "A", "B", "C", "D"
ShowControls True, "A", "B", "C", "D"
LockControls False, "A", "B", "C", "D"

End Sub

----------------------------------------------------

Private Sub cmdA_Click()
'disable tag A controls, hide tag B controls, lock tag C controls
EnableControls False, "A"
ShowControls False, "B"
LockControls True, "C"

End Sub


NOTE:
When using this code on a form, it is recommended that all controls have a tag value to ensure you have full control over their appearance.
Otherwise, controls with no tag may get hidden or locked or disabled as well!


Download

Click to download:      Set Controls       (zipped)


UPDATE 10 Nov 2019:
The Tag property has a wide range of possible uses in Access applications.

For example, APR Pillai (Learn MS Access Tips & Tricks) uses the tag property in this article: Duplicate Multiple Fields in a record to the next record with conditional formatting


Feedback

Please use the E-Mail button in the contact form below to let me know whether you found this article useful or if you have any questions.

Please also consider making a donation towards the costs of maintaining this website. Thank you



Colin Riddington           Mendip Data Systems                 Last Updated 29 Jan 2022



Return to Code Samples Page




Return to Top