How to find the controls on different forms or reports having same tag?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mshakeelattari
    New Member
    • Nov 2014
    • 100

    How to find the controls on different forms or reports having same tag?

    Is it possible to see if more than one labels (on different forms/reports) are having same tag property? Can someone help me?
  • isladogs
    Recognized Expert Moderator Contributor
    • Jul 2007
    • 479

    #2
    Loop through all the controls (or just the labels if you prefer), checking for those with a specified tag value.
    Then use Debug.Print ctrl.Name to display the names in the immediate window.

    Or add each control name to a text string and when done, display in a message box or output to a text file.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      Hi there.

      Are you asking about Controls with the same .Tag value across a single Form or Report? Or are you asking about such Controls which may be found across multiple Forms &/or Reports - as your question seems to imply?

      Certainly it helps to understand that, unlike Controls themselves, which have Collection objects within various other objects - like Forms, Reports, Sections, etc - the .Tag property is quite a basic one that contains simple text. So, whether you're looking within a single Form/Report - or even just one of their Sections - or across multiple Control Collections - the technique would be to cycle through the elements of (all) the Collection(s) and compare the .Tag value of each to match what it is you're looking for.

      In its simplest sense that would look something like :
      Code:
      For Each ctlVar In Me.Controls
          On Error Resume Next
          blnVar = False
          blnVar = ctlVar.Tag >= ""
          On Error GoTo 0     'or replace with whatever was used before.
          If ctlVar.Tag = "Check value" Then
              ' ... Your code here for when a match is found.
          End If
      Next ctlVar
      NB. Not all Controls even have a .Tag property so checking it for those would trigger an error that would need to be handled ;-)

      NB. Checking for Control type would mean not needing to do the error handling bit as only certain Control types have a .Tag property.

      Comment

      • mshakeelattari
        New Member
        • Nov 2014
        • 100

        #4
        Thank you @NeoPa for your response.

        Or are you asking about such Controls which may be found across multiple Forms &/or Reports - as your question seems to imply?
        Yes, the controls are to be compared across the forms and/or reports.

        NB. Not all Controls even have a .Tag property so checking it for those would trigger an error that would need to be handled ;-)

        NB. Checking for Control type would mean not needing to do the error handling bit as only certain Control types have a .Tag property.
        I need comparing the .Tag property of only Lable controls for which we can use If ctl.controltype = acLabel Then .

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Originally posted by mshakeelattari
          mshakeelattari:
          Yes, the controls are to be compared across the forms and/or reports.
          So, what are you actually talking about? What are you doing that needs to process through multiple Forms & Reports? What do you have already that you can fit the extra code within? Remember - we aren't here simply to do your work for you. I understand English is not your first language, but you should understand that point by now (99 posts already - and for many of those I've had to repeat this point).

          I can offer my earlier code modified to handle only Labels. That is pretty trivial :
          Code:
          For Each ctlVar In Me.Controls
              If ctlVar.ControlType = acLabel Then
                  If ctlVar.Tag = "Check value" Then
                      ' ... Your code here for when a match is found.
                  End If
              End If
          Next ctlVar

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            I created a Sub-Routine for you that will check every single Label in every Form, except the one passed to the Routine for a Tag Value that is also passed to the Routine. If a match, or matches are found, it prints the Form Name, Label Name, and Tag Value to the Immediate Window. This should be more than enough to point you in the right direction.
            Code:
            Public Sub CheckTagValue(strCurrentForm As String, strTag As String)
            Dim ctl As Control
            Dim aobFrm As AccessObject
            
            DoCmd.Hourglass True
            
            For Each aobFrm In CurrentProject.AllForms
              If aobFrm.Name <> strCurrentForm Then
                DoCmd.OpenForm aobFrm.Name, acDesign, , , , acHidden
                  For Each ctl In Forms(aobFrm.Name)
                    If ctl.ControlType = acLabel And ctl.Tag = strTag Then
                      Debug.Print aobFrm.Name; Tab(25); ctl.Name; Tab(75); ctl.Tag
                    End If
                  Next
                DoCmd.Close acForm, aobFrm.Name
              End If
            Next
            
            DoCmd.Hourglass False
            End Sub
            Code:
            Check all labels in all Forms, except Form3, for a Tag Value of 'Expenses2022'
            Call CheckTagValue("Form3", "Expenses2022")
            Code:
            Form1                   Label3                                            Expenses2022
            Purchases Subform for Purchase Order Details
                                    Unit Cost_Label                                   Expenses2022
            Order Subform for Order Details
                                    Product ID_Label                                  Expenses2022
            Order Subform for Order Details
                                    Status Name_Label                                 Expenses2022
            Customer Details        Last Name_Label                                   Expenses2022
            Customer Details        Notes_Label                                       Expenses2022
            Inventory List          Current Level_Label                               Expenses2022
            Shipper List            Company_Label                                     Expenses2022
            Sales Analysis Form     Product Name_Label                                Expenses2022
            Sales Analysis Form     Order Date_Label                                  Expenses2022
            Product Details         Target Level_Label                                Expenses2022
            Customers               Label6                                            Expenses2022
            Customers               Label24                                           Expenses2022
            Customers               Label30                                           Expenses2022
            Customers               Label39                                           Expenses2022

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Hi ADezii old friend :-)

              You may notice in the example code I gave earlier that I separated the check for Label & the value of .Tag into two If lines. This was no accident. Any Controls that have no .Tag property will crash in your example :-(

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                @NeoPa:
                Any Controls that have no .Tag property will crash in your example :-(
                Excellent point, one that I obviously overlooked. One question to you my friend, I executed this Code against every single Form in the Northwind 2007 Sample Database, plus some that I created (40 Forms in all/900+ Controls) with no Errors. How is this so, what have I obviously overlooked?

                Comment

                • isladogs
                  Recognized Expert Moderator Contributor
                  • Jul 2007
                  • 479

                  #9
                  @NeoPa
                  Sorry to tell you but both bound & unbound labels DO have a Tag property
                  It is true that labels don't have an Enabled or Locked property which may be what you were thinking of

                  @adezii
                  Does that answer your question?

                  Comment

                  • ADezii
                    Recognized Expert Expert
                    • Apr 2006
                    • 8834

                    #10
                    @isladogs:
                    Thanks, but I think that NeoPa was referring to non-Label Controls, all of which will be tested for each Form within the loop. Let's see what he has to say.

                    Comment

                    • isladogs
                      Recognized Expert Moderator Contributor
                      • Jul 2007
                      • 479

                      #11
                      Apologies - I misread the earlier comment and can see that @NeoPa was well aware the labels do have a Tag property.

                      If it is true that not all controls have a Tag value, then your code would indeed need to be adjusted for that.
                      However, I've previously checked all standard Access controls - all have a Tag property ...even a page break control.

                      I've only checked a few of the long list of ActiveX controls e.g. slider, animation, up/down, web browser, treeview ...
                      All of those also have a Tag property.
                      So I think you're probably safe with regard to the Tag property - though not if you were instead testing for e.g. Enabled
                      Last edited by isladogs; Aug 14 '22, 02:02 PM. Reason: Correction - page break not line break

                      Comment

                      • ADezii
                        Recognized Expert Expert
                        • Apr 2006
                        • 8834

                        #12
                        @isladogs:
                        I executed this Code against every single Form in the Northwind 2007 Sample Database, plus some that I created (40 Forms in all/900+ Controls) with no Errors.
                        Taking the above into consideration, I would also say that I should be safe with the one-line Statement. See Attached Image as far as to what Controls the Tag Property Applies To:
                        Attached Files

                        Comment

                        • isladogs
                          Recognized Expert Moderator Contributor
                          • Jul 2007
                          • 479

                          #13
                          Yes, I agree.
                          Also bear in mind that MS loves to put all its latest 'features' in their template databases, so I expect you've already tested most if not all the standard controls and possibly some of the ActiveX ones .....

                          I'd be interested to see the article you got that from.
                          Can you pass on the URL.

                          BTW it seems to omit some standard controls I tested e.g. WebBrowser
                          Last edited by isladogs; Aug 14 '22, 02:01 PM. Reason: Spelling & grammar

                          Comment

                          • ADezii
                            Recognized Expert Expert
                            • Apr 2006
                            • 8834

                            #14

                            Comment

                            • isladogs
                              Recognized Expert Moderator Contributor
                              • Jul 2007
                              • 479

                              #15
                              Thanks a lot.
                              Oops too short - minimum 20 characters

                              Comment

                              Working...