Build reference name(s) for multiple controls/buttons

vdc1

New member
Local time
Today, 13:19
Joined
Jun 29, 2008
Messages
4
I would like to show/hide a group of controls and buttons in access 03. Is there anyway of doing this without writting code for each individual control and button name. I have 12 controls and 5 buttons I would like to place into 3 groups, these groups are to show and hide depending on the data entering stage.

Instead of;
Me.ProductID.Visible = True
Me.CmdEditProductDetails.Visible = True
Me.TxtDescription.Visible = True
Me.TxtRetail.Visible = True

I would like to;
Me.ProductGroup.Visible = True
 
You can use the Tag property of the controls to specify a Group and then use something like:
Code:
Dim ctl As Control
For Each ctl In Me.Controls
   If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
       If ctl.Tag = YourNumber Then
          ctl.Visible = False
       Else
          ctl.Visible = True
       End If
   End If
Next ctl
 
That is absolutely standard.
At the risk of muddying the waters though - there are several ways to implement what amounts to very much the same thing.
Essentially - at one time or another you end up enumerating the controls collection.

Just for interest - consider the alternatives. Where we don't have to enumerate the controls collection each time...

For example you could store the controls names in a local table. A field for the properties of the controls. And when you request to hide them you iterate through the table recordset. We only ever act upon the requested controls - but we have the overhead of opening a recordset!
(This is shown in example ControlGroupsTbl.zip)

Some folks like to build a collection first - and add all the controls to the collection on form startup. This can be an ugly process (i.e. refering to the controls individually as they're added to the collection) or done by enumeration of all controls again (but you need some way of identifying each control - so we can fall back on the Tag property again ;-)
This then allows you to operate on the controls in a given collection only, instead of enumerating all controls on the form. (I think it's a bit cumersome myself - but see the example ControlGroupsCol.zip).

And you can extend that concept a bit.
If you encapsulate the logic into a simple class (or two) then you can have a fairly simple form set up and be free to implement as many distinct groupings as you like without any further complexity in the local form code (the classes then do the collections work). You still end up distinguishing the controls on the form using the Tag property and the code is loaded by one initial enumeration of all controls for convenience - but the result is nice and flexible for adding various methods and so on.
(See ControlGroupsCls.zip by way of example - don't worry about the classes if you're not into them - the local form code is the setup stuff).

As I said - hopefully not muddying the waters too much. But equally hopefully of interest to some.
 

Attachments

Glad you liked it Allan. Thought it was worth throwing out into the ether. :-)
 
Greatly appriciated!

Awesome examples Leigh, this helps a lot!
 

Users who are viewing this thread

Back
Top Bottom