Setting a group of controls visible/hidden or enabled/disabled (1 Viewer)

isladogs

MVP / VIP
Local time
Today, 02:14
Joined
Jan 14, 2017
Messages
18,221
As I'm sure many of you will already know it is easy to change a selected group of form controls to be visible or hidden with one line of code using the controls' Tag property.

For those who don't know how, add code like this to the form:

Code:
Private Sub SetControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
        Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)

On Error GoTo Err_Handler

    'set controls to visible or not according to the control tag value
    For Each ctrl In Me.Controls
        If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
            Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.visible = State
    Next ctrl
  
Exit_Handler:
    Exit Sub

Err_Handler:
    'create error message & log
    strProc = Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(Application.VBE.ActiveCodePane.TopLine, 0)
    PopulateErrorLog
    Resume Exit_Handler
    
End Sub

In the Form_Load event, add code to set the start up properties like this:
Code:
	SetControls True, "A"
    	SetControls False, "E", "S", "Q", "P", "V"
    	SetControls False, "F", "FB", "R", "FR", "RB"

Much quicker than writing e.g.
Code:
	Me.ctl1.Visible=True
	Me.ctl2.Visible=False
	Me.ctl3.Visible=True
	Me.ctl4.Visible=False
	Me.ctl5.Visible=False
	Me.ctl6.Visible=False
	Me.ctl7.Visible=True


When you need to change something, add code like this:
Code:
	SetControls True, "E", "S"
	SetControls False, "A"

However, it would be good to be able to to do a similar things to set a group of controls as enabled / disabled.
I've tried the following :

Code:
	Private Sub EnableControls(State As Boolean, Tg1 As String, Optional Tg2 As String, Optional Tg3 As String, _
        Optional Tg4 As String, Optional Tg5 As String, Optional Tg6 As String)

On Error GoTo Err_Handler

    'set controls to visible or not according to the control tag value
    'doesn't work
    For Each ctrl In Me.Controls
        If ctrl.Tag = Tg1 Or ctrl.Tag = Tg2 Or ctrl.Tag = Tg3 Or ctrl.Tag = Tg4 _
            Or ctrl.Tag = Tg5 Or ctrl.Tag = Tg6 Then ctrl.enabled = State
    Next ctrl
  
Exit_Handler:
    Exit Sub

Err_Handler:
    'create error message & log
    strProc = Application.VBE.ActiveCodePane.CodeModule.ProcOfLine(Application.VBE.ActiveCodePane.TopLine, 0)
    PopulateErrorLog
    Resume Exit_Handler
    
End Sub

with related code
Code:
	EnableControls True, "A", "N"
    	EnableControls False, "B", "C"

However, it doesn't work.
Anyone know a way of doing it?
 

static

Registered User.
Local time
Today, 02:14
Joined
Nov 2, 2015
Messages
823
Labels don't have enabled or locked properties, they will error.

If you use paramarray you aren't limited to the number of items you can pass in.

Also you could handle everything in a single sub instead of creating very similar subs for each property.

Code:
Public Enum PropChoice
    Visible
    Enabled
    Locked
End Enum

Public Sub setstate(state As Boolean, prop As PropChoice, ParamArray ctls())
    For Each c In ctls
        If c.ControlType <> acLabel Then
            Select Case True
                Case prop = Enabled: c.Enabled = state
                Case prop = Locked: c.Locked = state
                Case prop = Visible: c.Visible = state
            End Select
        Else
            Select Case True
                Case prop = Visible: c.Visible = state
            End Select
        End If
    Next
End Sub
 

isladogs

MVP / VIP
Local time
Today, 02:14
Joined
Jan 14, 2017
Messages
18,221
Hi

Thanks for this.

Hadn't considered using property constants in this context

I'll tweak your code a bit and try it out.
I agree about using a parameter array but had never got round to it for some reason

I wouldn't make it a public sub unless the form name is included as part of the sub when called. Obviously you don't want to globally lock all controls with the same tag. I'll add this change as part of my 'tweak'

Yes I know about label properties but they can be hidden
So in your code they don't need to be excluded if prop set as visible
 
Last edited:

static

Registered User.
Local time
Today, 02:14
Joined
Nov 2, 2015
Messages
823
I wouldn't make it a public sub unless the form name is included as part of the sub when called. Obviously you don't want to globally lock all controls with the same tag. I'll add this change as part of my 'tweak'

It's public so that you can call it from anywhere. Change sub to function and you could call it from a macro.
As a public procedure it wouldn't affect all controls only those that are passed into it. Also, I didn't implement anything to do with tags because I don't understand the point of using tags in this case.

Yes I know about label properties but they can be hidden
So in your code they don't need to be excluded if prop set as visible

You said your code didn't work, but with no clue what was wrong. I couldn't think of any reason why that would be, and the most obvious conclusion that I came to was that you were setting a label to disabled.

If you know that then, no you don't need the filter, but then again, a good shared procedure needs to error check and validate things ...
 

isladogs

MVP / VIP
Local time
Today, 02:14
Joined
Jan 14, 2017
Messages
18,221
You said your code didn't work, but with no clue what was wrong.

Sorry, I always complain when others do that
The answer is nothing happens - no error but no changes to selected controls

BUT I will go back & recheck I've got no labels with the selected tag !!!
I've been trying this on & off for a long time but you never know!

I didn't implement anything to do with tags because I don't understand the point of using tags in this case.

The point of using tags here is that they provide a very convenient way of grouping controls that you want to treat in the same way at particular points in your code
e.g. all 15 controls with tag B to be made visible here, all 20 controls with tag D to be hidden

Not yet tried your code let alone adapting it - will do so this evening
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:14
Joined
Jul 9, 2003
Messages
16,282
I did this video:- "Class Module enables multiple Command Buttons" back in 2010 so it's quite old! In other words don't expect me to recall exactly what I was talking about!

I feel it might be related to your question and thought you might be interested in having a look.

If that video is of interest, then the complete set of videos is Here:-

"Videos - Lock, Unlock Controls"

Along with a sample database...

Video N0# 1 demonstrates a simple method of unlock/locking a set of controls, to video No#7 demonstrating an object orientated approach. (The single video I posted above)
 

isladogs

MVP / VIP
Local time
Today, 02:14
Joined
Jan 14, 2017
Messages
18,221
Hi

Tony
Many thanks for the link - will look at it later.
As for class modules, I have the same problem with those I wrote years (or even months) ago.
No matter how well I add comments, I have to read class module code very slowly & carefully to understand them later.

Static
Apologies - it would appear that knowing labels can't be disabled and making use of that fact in my code are obviously 2 different things!

In most of my forms, many labels have been tagged the same as the accompanying textbox or other control.
Which means of course, this has been preventing my EnableControls code I posted in #1 working as planned

I'm glad I posted this as I doubt I'd have linked the 2 things myself in a hurry.
These things often just need another pair of eyes.

I've just made a test form with 4 buttons tagged A,B,C,D and labels NOT tagged
Each button disables/enables or hides/shows other buttons

It works perfectly though for some reason then triggers error 438 (Object doesn't support this property or method) in the 'new' EnableControls sub.
Easy to exclude that error but nevertheless odd.

No such error occurs in SetControls (visible/hidden) - the one I've used for 10+ years.

I'm going to combine your suggestions with mine & will post the definitive version probably tomorrow
 

isladogs

MVP / VIP
Local time
Today, 02:14
Joined
Jan 14, 2017
Messages
18,221
Hi

I've done some further work on this to check which control types can/can't be hidden, disabled or locked.

I thought others might find this information useful so have created a sample database to demonstrate controlling these 3 properties for all control types using the control tag property.

This can be found at:
https://www.access-programmers.co.uk/forums/showthread.php?t=293439

After some experimentation, I have stuck to my original approach but others may prefer Static's equally valid suggestion in post 2
 

Attachments

  • SetControls.PNG
    SetControls.PNG
    71.7 KB · Views: 269
Last edited:

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 02:14
Joined
Jul 9, 2003
Messages
16,282
I've just submitted it for approval & will add a link later if it is approved.

Hi ridders,
I've just approve both your post and attachments in the sample section.

There's a bunch of us here who do moderation, no one is in charge, it's just who ever happens to notice something wrong attends to it.

If something needs approval then who ever notices it approve it. Unfortunately this can lead to something that needs approval being missed.

It has been discussed in the moderators forum. One idea was that the person posting something in a moderated forum should "Report" their own post. In other words, press the report button and add a note saying please could this be approved.

This isn't really what the report function was made for, so we may change this idea later. However next time you post in a moderated forum please try this method as an experiment and we will see what happens!

I will link back to this thread from the moderators forum so that the other moderators can see what I have suggested.
 

isladogs

MVP / VIP
Local time
Today, 02:14
Joined
Jan 14, 2017
Messages
18,221
Thanks Tony - stupidly I wasn't aware you were a moderator
It really would help if one of you could post a list of moderators in e.g FAQs

The reason I've started contacting moderators is because my first 2 posts in a moderated area weren't picked up for over a week.
 

Users who are viewing this thread

Top Bottom