Is it possible to see if more than one labels (on different forms/reports) are having same tag property? Can someone help me?
How to find the controls on different forms or reports having same tag?
Collapse
X
-
Tags: None
-
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. -
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
.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
-
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?
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..Tag
property of only Lable controls for which we can useIf ctl.controltype = acLabel Then
.Comment
-
Originally posted by mshakeelattarimshakeelattari:
Yes, the controls are to be compared across the forms and/or reports.
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
-
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
-
Hi ADezii old friend :-)
You may notice in the example code I gave earlier that I separated the check forLabel
& the value of.Tag
into twoIf
lines. This was no accident. Any Controls that have no.Tag
property will crash in your example :-(Comment
-
@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
-
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. EnabledComment
-
@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 FilesComment
-
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. WebBrowserComment
-
Comment