First Published 28 Feb 2023           Last Updated 3 Mar 2023

This article shows some simple ways of using one or more tags on form (or report) controls to change the appearance, state or captions of the controls using code.

Tag values can either be set in the Other tab of the control property sheet or using code.
Any character or string of up to 2048 characters can be used.
If you use more than one tag value, separate these with e.g. a space, comma, semicolon or the 'pipe' symbol (|)

TagProperty
Here is some example code adapted from the Microsoft Tag Property help article to set/reset tag values and use these to set a label caption:

Sub Form_Load()
      Me.lblMessage.Caption = "This shows how tags can be used to set a caption value"
            & vbCrLf &"Click on the textbox or command button to see this work"

      Me.txtDescription.Tag = "Help text for the text box."
      Me.cmdButton.Tag = "Help text for the command button."
End Sub

Sub txtDescription_GotFocus()
      'Use tag property setting as caption
      Me.lblMessage.Caption = Me!txtDescription.Tag
End Sub

Sub txtDescription_LostFocus()
      Me.lblMessage.Caption = ""
End Sub

Sub cmdButton_GotFocus()
      ' Tag property setting as caption.
      Me.lblMessage.Caption = Me.cmdButton.Tag
End Sub

Sub cmdButton_LostFocus()
      Me.lblMessage.Caption = " "
End Sub


Form3 in the attached database demonstrates the use of the Tag property to set/reset caption values

Caption when textbox has focus

Form3B
Caption when button has focus

Form3C




The attached database also includes 2 versions of an unbound form with 6 textboxes. Each textbox has two or more tag values.

In design view, all textboxes are visible, enabled & unlocked with a white back color

DesignView
When opened in Form View, code in the Form_Load event sets the appearance and state of each textbox according to the tag values for each.

Code is used to display the tag values for each textbox as the caption of its attached label.

In addition, the textbox default value has been set to display what has been done to each textbox

Form1 uses *wildcards* to determine the appearance/state of each textbox.
For example, if the control tag is like "*H*", the control is hidden.

Dim ctl As Access.Control

Private Sub Form_Load()

      For Each ctl In Me.Controls

           'set label caption according to textbox tag values
            If ctl.ControlType = acTextBox And ctl.Tag <> "" Then
                  ctl.Controls(0).Caption = "Tags " & ctl.Tag
            End If

            'set appearance/state of all textboxes according to tag values using wildcards
            Select Case True

            Case ctl.Tag Like "*C*"
                  ctl.BackColor = vbCyan
            Case ctl.Tag Like "*G*"
                  ctl.BackColor = vbGreen
            Case ctl.Tag Like "*M*"
                  ctl.BackColor = vbMagenta
            Case ctl.Tag Like "*H*"
                  ctl.Visible = False
            Case ctl.Tag Like "*L*"
                  ctl.Locked = True
            Case ctl.Tag Like "*D*"
                  ctl.Enabled = False

            End Select
      Next

      'restore button settings
      cmdReset.Enabled = True
      cmdReset.Visible = True
      cmdClose.Caption = "&Close Form"

      'set MDS caption in footer
      Me.lblMDS.Caption = "Mendip Data Systems 2005-" & Year(Date)

End Sub


The Form_Load code has this effect when the form when opened

Form1View
Click the Reset button to remove all effects caused by the Form_Load event code

Private Sub cmdReset_Click()

      'reset all textboxes
      For Each ctl In Me.Controls

            If ctl.ControlType = acTextBox Then
                 ctl.Enabled = True
                 ctl.Visible = True
                 ctl.Locked = False
                 ctl.BackColor = vbWhite
                ctl.Value = "Enabled, visible, unlocked, white back color"
            End If

     Next

     'disable then hide button
     cmdReset.Enabled = False
     cmdReset.Visible = False

     'change cmdClose button caption
     cmdClose.Caption = "&Reload Form"
End Sub


The Close button caption now changes to Reload Form. Click to restore all effects using the Form_Load code

Form1ViewAfterReset

Private Sub cmdClose_Click()

     If cmdClose.Caption = "&Close Form" Then
          DoCmd.Close
      Else
          'caption - "&Reload Form"
          Form_Load
     End If

End Sub



The above code will work perfectly provided all tag values can be correctly disambiguated using wildcards

However, if three different controls have e.g. tags H, H1 and ZH, using the wildcard Like "*H*" means all will be hidden, which may not be what you want.

A much better approach is to use the split function with a string array to correctly identify each value in the control tag.

Form2 uses the Split function approach

Form2View

Dim ctl As Access.Control

Private Sub Form_Load()

      Dim arrSplitTag() As String, I As Integer

      For Each ctl In Me.Controls

           'set label caption according to textbox tag values
            If ctl.ControlType = acTextBox And ctl.Tag <> "" Then
                  ctl.Controls(0).Caption = "Tags " & ctl.Tag
            End If

            'set appearance/state of all textboxes according to tag values using split function
            'similar tags will be diambiguated e.g. H will be hidden but H1 won't
            arrSplitTag = Split(ctl.Tag, ",")
            For I = LBound(arrSplitTag) To UBound(arrSplitTag)
                  Select Case arrSplitTag(I)

                  Case "C"
                        ctl.BackColor = vbCyan
                  Case "G"
                        ctl.BackColor = vbGreen
                  Case "M"
                        ctl.BackColor = vbMagenta
                  Case "H"
                        ctl.Visible = False
                  Case "L"
                        ctl.Locked = True
                  Case "D"
                        ctl.Enabled = False

                  End Select
      Next

      'restore button settings
      cmdReset.Enabled = True
      cmdReset.Visible = True
      cmdClose.Caption = "&Close Form"

      'set MDS caption in footer
      Me.lblMDS.Caption = "Mendip Data Systems 2005-" & Year(Date)

End Sub




Download

Example database: Multiple Tags     ACCDB file     Approx 0.37 MB (zipped)



Summary

I hope this article has given you some ideas for using the tag property.

The tag property offers numerous possibilities for customising forms and reports - you are only limited by your imagination

See my related article: Set Controls. This demonstrates how to set the state of a group of controls at the same time using the controls' Tag property.



Feedback

Please use the contact form below to let me know whether you found this article interesting/useful or if you have any questions/comments.

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



Colin Riddington           Mendip Data Systems                 Last Updated 3 Mar 2023



Return to Access Blog Page




Return to Top