'Serving the Microsoft® Access
Community since 1997'

Free Samples
About Us



Looping Through Form Objects Using the Tag Property

In another article (click here to read it) I described how to loop through similarly named objects on a form, or objects that have the same names as the recordset in use.

This article will show how to do it with seemingly unrelated objects.

Scenario: We have a form with a number of text boxes, combo boxes, list boxes, etc. If a user clicks on a particular button, some of the objects get locked, or change background colour, or ...

(NOTE: this article was written based on research using Access 2000. It should work on other versions.)

NOTE: All the VBA code segments on the Database Lessons site assume that you have DAO references active. If you are not sure what this means, and you are using Microsoft Access 2000 or higher, click here.

Step 1. Open the property box for an object that you will want included in the logic.

Step 2. Change the "Tag" property to have a code. For example, if you will be locking the field, put the word "lock" in the Tag property.

(article continues after sponsor spot)

Step 3. Repeat for all required objects.

Step 4. Create the required button and name it properly.

Step 5. On the Events tab of the command button's properties, choose the 'On Click' property and create an event for it.

Step 6. Enter the following VBA code.

    Dim cx As Control
    '--- turn off error handling as some objects may not have the Tag property
    On Error Resume Next
    For Each cx In Me.Controls
        If cx.Tag = "lock" Then cx.Locked = True
    Next cx

Test, and if all is okay, you are done.

Note: This web site dedicated to MS Access database users is an independent publication of Richard W. Killey and is not affiliated with, nor has it been authorized, sponsored, or otherwise approved by Microsoft® Corporation.


© 2006, 2007, 2008 Richard W. Killey. All Rights Reserved. - Privacy Policy