Solved Multiple tags on a control (1 Viewer)

Romio_1968

Member
Local time
Today, 08:56
Joined
Jan 11, 2023
Messages
126
I need to take group action on controls on a form, based on their tag
Let say, some of them I want to be hidden on load. I will assign them a tag "H"
For other, I want them to be disabled. Tag "D"
Other set to Null value.

So some of them may be similtaneously in more than one group of action
Let say that oane should be Nulled and hidden
I tried to assign two tags, sepparated by semicolon (tried also comma, dot,,,)

I created a function called ActionTag that will loop trough the controls and retrieve the tag.
Insede the function, a Case Select is set, and for each case the appropiate action is taken.

Works like a charm for one tag, but when come to multiple, the function ise dead. Nothing happens.
I tried to treat the Tag property content as an string and splitt it in an array.
Still nothing.

Any ideea how this can be done.

(see Mr. Hartman, you put my soul on fire now :) )

------------------------------
RESOLUTION
Don't know yet what was going wrong but probably it was some typo or ill set property, call or event.
Also it is possible that a reddundancy in the old code may have lead to the error (I was rechecking for a Tag inside the Case for tahat Tag)

Here is an example of the code that works
Code:
Public Sub ActByTag(frm As Form)
  Dim ctl As Control
  Dim tags As Variant
  Dim tag As Variant
  
  For Each ctl In frm.Controls
    tags = Split(ctl.tag, ";")
    For Each tag In tags
      Select Case tag
        Case "Clear"
          ctl.Value = ""
        Case "Hide"
          ctl.Visible = False
      End Select
    Next tag
  Next ctl
End Sub

Thank You theDBguy for testing
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:56
Joined
Oct 29, 2018
Messages
21,473
Can you post the code you're using?
 

Romio_1968

Member
Local time
Today, 08:56
Joined
Jan 11, 2023
Messages
126
Can you post the code you're using?
Public Sub ActByTag(frm As Form)
Dim ctl As Control
Dim tags As Variant
Dim tag As Variant


For Each ctl In frm.Controls
tags = Split(ctl.Tag, ".")
For Each tag In tags
Select Case tag
Case "Clear"
ctl.Value = ""
Case "Hide"
If TypeName(ctl) = "SubForm" Then
If ctl.Tag = "Hide" Then
ctl.Visible = False
End If
ElseIf ctl.Tag = "Hide" Then
ctl.Visible = False
End If
End Select
Next tag
Next ctl
End Sub



The Call is in the Form_OnLoad event

ActByTag Me

Again, works nice for single tag

In this case the tag property of control is set to: Hide.Clear I also tried "Hide.Clear", "Hide"."Clear" and various dividers
 

Gasman

Enthusiastic Amateur
Local time
Today, 06:56
Joined
Sep 21, 2011
Messages
14,299
I would use Instr() to find the tag?
However what is meant to happen if you have two tags for the same process?

I was only thinking of a tag to hide/display and a tag to make red/blue etc?
Each tag was afor a different reason.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:56
Joined
Feb 28, 2001
Messages
27,186
I need to take group action on controls on a form, based on their tag
Let say, some of them I want to be hidden on load. I will assign them a tag "H"
For other, I want them to be disabled. Tag "D"
Other set to Null value.

So some of them may be similtaneously in more than one group of action
Let say that oane should be Nulled and hidden
I tried to assign two tags, sepparated by semicolon (tried also comma, dot,,,)

I created a function called ActionTag that will loop trough the controls and retrieve the tag.
Insede the function, a Case Select is set, and for each case the appropiate action is taken.

Works like a charm for one tag, but when come to multiple, the function ise dead. Nothing happens.
I tried to treat the Tag property content as an string and splitt it in an array.
Still nothing.

Any ideea how this can be done.

(see Mr. Hartman, you put my soul on fire now :) )

You can separate the tags from each other with any character like ";" or "|" or "," as you wish. The TAG property doesn't inherently do anything to the appearance of the field - unlike, say, the FORMAT or FORECOLOR properties.

Then, as suggested by Gasman, use INSTR to see if a particular tag is in use for that control. The other possibility is to use the SPLIT function and process all of the tags according to which ones are present. Note that neither method requires a single-character tag.



Either way, you are looking at a complex bit of code. You can use what is called an "IF LADDER" or a "SELECT CASE" sequence to decide what to do for a given tag, but there is this wrinkle. Both the IF LADDER and SELECT CASE, when done traditionally, will only handle one tag. You would have to loop across all possible tags IF you wanted multiple tags to apply.

If you choose, you can test for each possible tag one at a time using INSTR, and if the tag is present, implement the effect. OR you can use SPLIT to find each tag and implement that tag if you found it. The difference is that using the INSTR method, you have to test for every possible tag. Using the SPLIT method, you can simply act on the tags you found, but have to loop through the tags until you run out of them.

And for the record, Pat Hartman is Ms. Hartman.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:56
Joined
Oct 29, 2018
Messages
21,473
In this case the tag property of control is set to: Hide.Clear I also tried "Hide.Clear", "Hide"."Clear" and various dividers
Hi. I just gave it a try, and it works for me. I just used the first one: Hide.Clear

1675613034606.png
 

Romio_1968

Member
Local time
Today, 08:56
Joined
Jan 11, 2023
Messages
126
Hi. I just gave it a try, and it works for me. I just used the first one: Hide.Clear
can you lend me your laptop, then? :)

It is crazy... i don't really know what was wrong, but it works. I modified the Hide;Clear to H;C (anyway it is suppose to be the same thing) and used ; as divider. I will test some more.
This kind of things bugs me off :)
Yet, is fun

By the way, since I buildt this procedure from others, it seems that I also did a mistake by rechecking for Hide (H.. whatever) inside the Case Hide, which is redundant at least

Here is the new Code
Code:
Public Sub ActByTag(frm As Form)
  Dim ctl As Control
  Dim tags As Variant
  Dim tag As Variant
  
  For Each ctl In frm.Controls
    tags = Split(ctl.tag, ";")
    For Each tag In tags
      Select Case tag
        Case "Clear"
          ctl.Value = ""
        Case "Hide"
          ctl.Visible = False
      End Select
    Next tag
  Next ctl
End Sub
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 06:56
Joined
Jan 14, 2017
Messages
18,221
I've just tested with multiple tag values such as A, H, X, Y.

Another approach is to use wildcards with If..Else..End If
For example
Code:
Private Sub Form_Load()

Dim ctrl As Access.Control

For Each ctrl In Me.Controls

If ctrl.Tag Like "*H*" Then
    ctrl.Enabled = False
ElseIf ctrl.Tag Like "*A*" Then
    ctrl.Visible = False
End If

Next

End Sub

Unfortunately that isn't possible using Select Case statements which don't allow wildcards
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 22:56
Joined
Oct 29, 2018
Messages
21,473
Unfortunately that isn't possible using Select Case statements which don't allow wildcards
One could try the following syntax:
Code:
Private Sub Form_Load()
Dim ctl As Access.Control

For Each ctl In Me.Controls
    Select Case True
        Case ctl.Tag Like "*H*"
            ctl.Enabled = False
           
        Case ctl.Tag Like "*A*"
            ctl.Visible = False
       
    End Select
   
Next

End Sub
 

Romio_1968

Member
Local time
Today, 08:56
Joined
Jan 11, 2023
Messages
126
One could try this syntax:
Code:
Private Sub Form_Load()
Dim ctl As Access.Control

For Each ctl In Me.Controls
    Select Case True
        Case ctl.Tag Like "*H*"
            ctl.Enabled = False
           
        Case ctl.Tag Like "*A*"
            ctl.Visible = False
       
    End Select
   
Next

End Sub
Yes... It works and it is updated in the original post. Thank You
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 01:56
Joined
May 21, 2018
Messages
8,529
Trim your results when you split.
It may not have worked because you did
H; C
Not
H;C
This will not equal "C" it equals " C"
 

CJ_London

Super Moderator
Staff member
Local time
Today, 06:56
Joined
Feb 19, 2013
Messages
16,612
since the properties concerned are booleans you could just use

Code:
Dim ctl As Control
For Each ctl In Me.Controls
    ctl.Enabled = not (ctl.Tag Like "*D*")'disable control if D
    ctl.Visible = not (ctl.Tag Like "*H*")'hide control 
Next ctl

This assumes you are using letters rather than words, but will still work if the letters are unique within the whole tag value (wouldn't be for 'Disable/Hide')

or use

ctl.Enabled = not (ctl.Tag Like "*Disable*")'disable control
ctl.Visible = not (ctl.Tag Like "*Hide*")'hide control
 

isladogs

MVP / VIP
Local time
Today, 06:56
Joined
Jan 14, 2017
Messages
18,221
Just tested the solution DBG suggested and it does indeed work.
For years, I've known that Select Case doesn't work with Like & wildcards but had never seen such as a simple solution for overcoming that limitation

So, whilst other solutions are possible in this particular case including If...Else..End If and the code suggested by CJ_London, the workround is going to have many other uses in the future.

Thanks DBG. I'll write a web article about this acknowledging you as the source of the code
 

Users who are viewing this thread

Top Bottom