Results 1 to 7 of 7
  1. #1
    Join Date
    May 2010
    Posts
    23

    Unanswered: Setting Control Tags

    Hi guys,

    I am using the following loop to set tags for a group of checkbox controls I'd like to group together.
    Code:
    For Each Ctrl In Me.Controls
    MsgBox Control.Name
        If TypeOf Ctrl Is CheckBox Then
        Ctrl.Tag = "rptField"
        End If
    Next
    If I include
    Code:
    MsgBox Ctrl.Tag
    in my loop, it appears that everything is working successfully.
    Unfortunately, the tag of each of these controls does not seem to stick after the loop is complete. i.e. After I run the code the tag property is set back to being blank.
    Any clue?

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    When you write that the tag property is set back to being blank, if you mean that they are blank the next time you open the form, this is normal. When you dynamically change the value of the property of a control on a form (i.e. if you use a VBA assignement instruction to modify the value when the form is open in Form view) the value is not kept when the form is closed, except if you explicitly use the instruction:
    Code:
    DoCmd.Close acForm, Me.Name, acSaveYes
    for closing the form and that the AllowDesignChanges property of the form is set to True.

    You can also use a procedure in another module and handle the form in Design view:
    Code:
    Sub SetTags()
    
        Dim frm As Form
        Dim ctl As Control
        
        DoCmd.OpenForm "<FormName>", acDesign   ' Replace <FormName> with the actual name of the form.
        Set frm = Forms("<FormName>")           ' Replace <FormName> with the actual name of the form.
        For Each ctl In frm.Controls
            If TypeOf ctl Is CheckBox Then
            ctl.Tag = "rptField"
            End If
        Next ctl
        DoCmd.Close acForm, frm.Name, acSaveYes
        
    End Sub
    Notice that if the sole purpose of your using the Tag property is to group a set of controls together for treating them as a set afterwards, there is a more efficient method that uses a Collection:
    a) In the Declarations section of the Form module:
    Code:
    Private m_colrptField As Collection
    b) Adding the desired controls to the collection:
    Code:
    Private Sub Form_Open(Cancel As Integer)
    
        Dim ctl As Control
        
        Set m_colrptField = New Collection
        For Each ctl In Me.Controls
            If ctl.ControlType = acCheckBox Then
                m_colrptField.Add ctl
            End If
        Next ctl
        
    End Sub
    c) You can then handle the collection as a set of controls:
    Code:
    Dim ctl as Control
    
    For Each ctl In m_colrptField
        ctl.Value = False
    Next ctl
    Or:
    Code:
    Dim i as Integer
    
        For i = 1 To m_colrptField.Count
            m_colrptField(i).Value = False
        Next i
    Have a nice day!

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    I tried doing this sort of thing years ago, and attempted to just now, to test what I remembered, which its that it has to be done with the Form open in Design View, in order for the changes to 'stick.'

    Perhaps the pertinent question is why you're attempting to do this, i.e. what's your underlying need, here? Perhaps we can offer a different approach.

    BTW, you do have to use

    DoCmd.Close acForm, Me.Name, acSaveYes

    but it has to be executed in Design View, and if you go looking for the AllowDesignChanges Property in 2007/2010 you won't find it! I understand that it's still valid, and can be set in code, it just doesn't appear in the Properties Pane anymore. Also, in these versions it is set to True by default.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Join Date
    May 2010
    Posts
    23
    Great explaination Sinndho and linq. Really appreciate it!
    I am actually writing this routine just as a quick way to name all my checkboxes instead of doing it manually one by one. ...There's a lot of boxes on this form!

  5. #5
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Then why do you need to do it dynamically, with code, instead of in Design View? If it's because of the large number of Controls you need to do this to, you can change the Tag Properties of multiple Controls in one fell swoop:
    • Go into Form Design View
    • Hold down the <Shift> Key and keep holding it down
    • Click on each Control whose Tag you want to set
    • Go to Properties - Other
    • Enter your desired Tag (no quotes, please!")
    • Exit the Properties Pane
    Now all of those Controls are 'tagged' the same!

    If some Controls need to have different Tags, at different times, i.e. belong to more than one 'group,' you can actually assign multiple Tags to a Control.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    Join Date
    May 2010
    Posts
    23
    I just got schooled. Thank you very much!

    Even though I feel dumb for not thinking of this simple solution, I'm glad I posted here because I did learn from both of your helpful explanations on "acSaveYes" and building "Collections".

  7. #7
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    No reason to feel dumb! I've been doing this for over a dozen years and learn something every day! We're all learning!

    Good luck with your project!

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •