Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping Controls 1

Status
Not open for further replies.

cneill

Instructor
Mar 18, 2003
210
GB
Hi
Is there a way of making a group of controls invisible? at the present time I am doing the following:-
e.g.
Public Sub HideSocial()

'Hide Social Items
Forms![FrmSchemes].ForecastGWh.Visible = False
Forms![FrmSchemes].ForecastppkWh.Visible = False
Forms![FrmSchemes].LabelGWhDelivered.Visible = False
Forms![FrmSchemes].GWhDeliveredLabel.Visible = False
Forms![FrmSchemes].LabelGWhBalance.Visible = False
Forms![FrmSchemes].GWhBox.Visible = False
Forms![FrmSchemes].GWhDelivered.Visible = False
Forms![FrmSchemes].GWhBalanceRemaining.Visible = False
Forms![FrmSchemes].ForecastNoofSocialMeasures.Visible = False
Forms![FrmSchemes].ForecastNoofMeasures.Visible = False
Forms![FrmSchemes].LabelUValueto045.Visible = False
Forms![FrmSchemes].ForecastNoofMeasures1.Visible = False
Forms![FrmSchemes].LabelUValueto037.Visible = False
Forms![FrmSchemes].ForecastNoofMeasures2.Visible = False
Forms![FrmSchemes].LabelUValueto035.Visible = False
Forms![FrmSchemes].FuelSwitchID.Visible = False
Forms![FrmSchemes].FuelSwitchID1.Visible = False
Forms![FrmSchemes].FuelSwitchID2.Visible = False
Forms![FrmSchemes].PG.Visible = False
Forms![FrmSchemes].TRV.Visible = False
Forms![FrmSchemes].ForecastTCO2.Visible = False
Forms![FrmSchemes].Forecast£tCO2.Visible = False
Forms![FrmSchemes].CustomerContribution.Visible = False
Forms![FrmSchemes].LabelAddNewInvoices.Visible = False
Forms![FrmSchemes].IDStatus.Visible = False
Forms![FrmSchemes].SocialSchemeDiscription.Visible = False
Forms![FrmSchemes].LabelTCO2Delivered.Visible = False
Forms![FrmSchemes].Box424.Visible = False
Forms![FrmSchemes].TCO2DeliveredLabel.Visible = False
Forms![FrmSchemes].LabelTCO2Balance.Visible = False
Forms![FrmSchemes].£ForecastValue.Visible = False
Forms![FrmSchemes].TCO2Delivered.Visible = False
Forms![FrmSchemes].TCO2BalanceRemaining.Visible = False

End Sub
So I call HideSocial and this runs this Public sub, is there a better/simpler way of doing this?
As background this example runs when the user makes a selection from a combo box, there are 12 diferent groups, the point is to make sure only the required controls are visible for the selection that has been made.
Many thanks neill
 

You can use the Tag property.

Something like...
Code:
Dim ctl As Control
For Each ctl in Me
    If ctl.Tag = "Hide me" Then
        ctl.visible = False
    End If
Next ctl


Randy
 
Will adding a tab control on the form help? For example, tab 1 would contain the controls for the 1st group, tab 2 for the 2nd group, etc. Now, just programmatically select the tab that represents the group. Of course, hide the tabs so the user can click on them.
 
How are ya cneill . . .

Put a question Mark [blue]?[/blue] in the [blue]Tag[/blue] property of each control of interest ( no quotations please!). In the code module of the form, remove your [blue]HideSocial[/blue] routine then copy/paste the following in its place:
Code:
[blue]Public Sub HideSocial(flg As Boolean)
   [green]'flg = True = Show
   'flg = False = Hide[/green]
   
   Dim ctl As Control
   
   For Each ctl In Me.Controls
      If ctl.tag = "?" Then ctl.Visible = flg
   Next

End Sub[/blue]
When you call the code just specify True/false to Show/Hide the controls.
Code:
[blue]   Call HideSocial([purple][b]True[/b][/purple]) [green]'Show[/green]
   Call HideSocial[purple][b]False[/b][/purple]) [green]'Hide[/green][/blue]

[blue]Your Thoughts? . . .[/blue]

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Hi TheAceMan1

this looks interesting, I will investigate further now that I have been pointed me in the right direction.
I will feed back
thanks for everyone help

Neill
 
Can I have multiple references in the TAG
i.e.

If ctl.tag = "1" or "2" or "3" Then ctl.Visible = flg

Thanks Neill
 
if ctl.tag = "1" or ctl.tag = "2" or ctl.tag = 3

or

select case ctl.tag
case "1","2","3"
ctl.visible = flg
case "5", "6"...
ctl.visible = not flg
end select
 
I would look at a tab control as already mentioned and hide/show the pages.

This may be overkill for what you want, but another approach is to define collections for each group. I like to do this when working with multiple groups of controls

Code:
Public groupOne As New Collection
Public groupTwo As New Collection
Public groupThree As New Collection

Private Sub Form_Load()

  'Load the controls into their own collection
  Dim ctl As Access.Control
    For Each ctl In Me.Controls
      If ctl.Tag = "1" Then
        groupOne.Add ctl, ctl.Name
      ElseIf ctl.Tag = 2 Then
        groupTwo.Add ctl, ctl.Name
      ElseIf ctl.Tag = 3 Then
        groupThree.Add ctl, ctl.Name
      End If
    Next ctl
End Sub


Public Sub HideGroup(group As Collection)
  'Now you can use a for each on a collection
  Dim ctl As Access.Control
  For Each ctl In group
    ctl.Visible = False
  Next ctl
End Sub

Public Sub ShowGroup(group As Collection)
  Dim ctl As Access.Control
  For Each ctl In group
    ctl.Visible = True
  Next ctl
End Sub

Private Sub Command28_Click()
  'Call it this way 
  HideGroup groupOne
  HideGroup groupTwo
End Sub

Private Sub Command29_Click()
  ShowGroup groupOne
End Sub

 
Hi MajP

The Collection method works thanks, but so does
if I use if ctl.tag = "1" or ctl.tag = "2" or ctl.tag = 3 etc

Which do you think is the fastest option using broadband when useing a back end/front end access database

Collection
or
if ctl.tag = "1" or ctl.tag = "2" or ctl.tag = 3

Thanks

Neill
 
I can not imagine their could be a noticeable difference, but theoretically the collection could be faster.

Assume you have 500 controls on a form but each group only uses 50.

With my method you one time run through the form controls collection to load the collections

For each ctl in forms("Frmscheme").controls (which is 500 iterations)
every subsequent for each on a user defined collection only runs 50 iterations and no boolean checks.

With the other method everytime you run through the entire form controls collection. All 500 iterations run an if then check.

With that said I still doubt you could notice any speed difference.

This is in theory.
 
Hi MajP

I will stick with the collection, as it works, having said that I just got this error message

"Runtime Error '457': This Key is already associated with an element of this Collection."

Have you had this before? do you know the solution?

Thanks Neill
 
Yes every key has to be unique. I used the controls name, as the key, but I think if you leave it blank it assigns the index as the key
 
Hi MajP

The first time I load the form, I don't get the error, when I close the form then open it again, I get the error.
Could I empty the Collections on exit of the form? if so how do I do that?

Thanks

Neill
 
My guess is that you are declaring your collection variables outside of the form so those variables are global and do not go out of scope when the form closes.

If you can I would look at declaring them as local to the form, you always want to limit the lifetime and scope of your variables.

But if you want to have these as global variables (they persist after the form closes) then two options

To clear a collection (remove the items)

Public Sub clearCollection(col As Collection)
Dim intItem As Integer
For intItem = (col.Count) To 1 Step -1
col.Remove (intItem)
Next intItem
End Sub


Or to set the collection to nothing.

Public Sub setCollectionNothing(col As Collection)
Set col = Nothing
End Sub

The clear method allows you to refill a collection without setting it back to something. In the latter you have to set it again.

But either way I would at making these collections local to the form if possible. No need to keep the variable once the form closes.
 
Hi MajP,

Moved the code inside form, problem fixed.

one minor problem this has caused, part of the code was public so I called it from a pop up form (this pop up opened from the main form we have been talking about)
Now that it is private I can't call it. Can I call the private sub from the pop up? I guess not, I don't really want to dublicate the code in a Public sub. I tried making the sub public in the form but no joy.
any thoughts?

thanks

Neill
 
A forms module or reports module is a Class module like any other class module.

lets assume we have a custom class called Widget

The normal process to work with an object of a class is to dimension the object, instantiate the object. Something like

some sub
dim wdj as Widget
Set wdj = new Widget
end sub

now you have a Widget object called wdj. To use a procedure of a Widget class you simply do something like

wdj.widgetProcedure someParameter
or

Once you understand that it is the same for procedures in a form module. So in order to use a procedure inside a form module you need a reference to the object.

There are a couple of ways.

1. Use the forms collection to get the form object
Forms("YourFormName").yourProcedure

2. Use the actual form's class
Form_YourFormName.yourProcedure

Example

in a form called frmOrders

Public Sub helloWorld()
MsgBox "HelloWorld"
End Sub

I can call that procedure from anywhere (assuming the form is open).

Form_frmOrders.helloworld
forms("frmOrders").helloworld

An event procedure is just a procedure so you can call that from the outside as well assuming it is public

in form
Public Sub Form_Current()
outside of form

Form_frmOrders.Form_Current

 
Hi Majp,

thank for the lesson, all sorted, thankyou for your time

Neill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top