Hi, I want to disable in VBA (.enabled = false) a lot of controlls, depending of some conditions. Is it possible to group (and having an unique name)these controlls to disable them all together at the same time.
I'm not the best to answer this but how about a For Next loop.
I have controls called FR1, FR2, FR3, FR4, FR5. To loop through and change them, I use this;
For i = 1 To 5
Me.Controls("FR" & i).Enabled = False
*more code here
Next i
Don't forget that you'll need code to enable them also.
It is not really possible to group controls in the way you might want. However, I have had to deal with this same type thing over the years and have come up with what I thing is a good method for accomplishing the desired results.
I utilize the "tag" property of each control that I need to manage. For each control that I want to "disable" or "enable" I put "SetEnabled". I do the same for controls that I want to have be visible by putting "SetVisibility". I use the same idea for controls that I want to "Lock" by putting "SetLocked" in the tag property. I then use a public function that I have written that evaluates these values in my form or sub form and sets or changes the various values of each control when the function is called.
On the same way, you can use the TAG property.
Set the .tag property to the same value then use a cycle to enable/disable the controls that have the same tag:
Code:
Dim ctl As Control
For Each ctl in Controls
If ctl.tag = "CommonTags" Then
ctl.Enabled = True/False
End If
Next ctl
Note, please, that this is an "in air" code. So, maybe, is necessary to tweak it a little bit.
Good luck !
Glad to help you.
I must ask Mr.B to forgive me because I give the same solution as he.
But... I started to answer then my dog "asked" me to go out. And he "say": NOW.
When I returned I continued the answer and I post it. Only at that time I saw Mr.B answer.
Since the OP already has a solution this is for interest only...
You can group controls. You can do this by using the tab control.
Setting the Style of the tab control to None removes the tabs and then setting the back style to transparent makes the control "invisible" so the user is not aware that a tab control is being used. Add the controls you want to group to the tab page.
The code for iterating your controls in the tab control is:
Dim ctrl As Control
For Each ctrl In Me.myTabPage.Controls
'your code to change the control here
Next