Access MVP (2010-2015)

oOo.oOo.oOo.oOo.oOo.oOo

.Tag, you’re it…

The .Tag Property, not to be confused with Smart Tags,  of a Control is probably the most under used and confusing Property of all.  So what do you do with it?  Here’s some examples…

1. Use it to Lock bound Controls…

Dim ctl As Control
Dim frm As Form

For Each ctl In frm.Controls
If ctl.Tag = "LockMe" then
   ctl.Locked = True
End If
Next ctl

Then any Control that has LockMe as a Tag will be locked.

You want to put it in a Module and call it in the After_Update of the control but you should also consider a way to unlock in case corrections have to be made.

2. Use it to validate that data has been entered in a specified field…

Public Function frmValidateData() As Boolean
On Error GoTo ErrHandler

    Dim ctl As Control
    Dim blnValid As Boolean
    Dim frm As Form

    Set frm = Screen.ActiveForm
 
  blnValid = True
 
  For Each ctl In frm.Controls 'Only use if no subform involved
    If ctl.Tag <> "" Then
      If ctl.Enabled Then
        If InStr(1, ctl.Tag, "require") Then
          If Nz(ctl, "") = "" Then
            blnValid = False
            MsgBox (ctl.Name & " cannot be empty.")
            ctl.SetFocus
            GoTo Complete
          End If
        End If
      End If
    End If
  Next ctl
 
Complete:
  Set ctl = Nothing
  frmValidateData = blnValid
  Exit Function

ErrHandler:
  blnValid = False
  MsgBox ("Error validating: " & Err.Description)
  Resume Complete
End Function

Tag

3. Use it to Lock a Field after it’s been edited…

Posted by Ken Sheridan in the Microsoft Answers Forum in this thread Lock Field after Edit?
A variation on Gina’s solution would be to put the following in the form’s Current event procedure:

Dim ctl As Control

For Each ctl In Me.Controls
     If ctl.Tag = "LockMe" then
        ctl.Locked = Not Me.NewRecord
     End If
Next ctl

When you navigate to an empty new record the tagged controls will be unlocked so data can be entered, but when you navigate to a previously entered record they’ll be locked.  If you did want to add an ‘Enable Edits’ button to the form so that existing records can be edited the code for the button’s Click event procedure would be:

Dim ctl As Control

For Each ctl In Me.Controls
     If ctl.Tag = "LockMe" then
        ctl.Locked = False
     End If
Next ctl

4. Use it to Call a Module…

Posted by Doug Steele in the Microsoft Answers Forum in this thread Use Tag Property of Control to Call Module

One approach would be do create a function in the form’s module along the lines of:

Function HandleClickEvent()

     Select Case ActiveControl.Tag
        Case "DoThis"
        Call MyModule
        Call "Do That"
        Call MyOtherModule
     Case Else
        ' Do nothing
     End Select

End Function

Now, you can set the On Click property of each of the 4 buttons to =HandleClickEvent(), rather than [Event Procedure]. Note that for this to work, it must be a function (not a sub), and you must include the equal sign and parentheses.

5.  Use it to Lock (and color) specific fields on a Continuous Form.  In this case I only wanted to Lock the ones that had data in them.  So first, under Conditional Formatting…

ConditionalFormatting

I prefer to use Cream to indicate the field is Locked but you can select any color OR any other formatting style you like.  Then add *LockMe* (no asterisks) to the the Tag Property in the Controls Property window.  And finally, add the below to the On_Current event of my Continuous Form…

For Each ctl In Me.Controls
     If ctl.Tag = "LockMe" And ctl.Value <> "" Then
        ctl.Locked = True
     End If
Next ctl

All Done!  Now, only the Controls that have data (and any other formatting you have applied) will be Locked which will also apply once data is entered into them.  Immediately apply Undo and your User can *Unlock* the record…  This is a biggie, so you might want to make a way for your User to either Undo or Edit the record should they decide at a later date to do so!

 1,435 total views,  1 views today

Comments are closed.