use ctl.tag to hide controls on active form/subforms.

Need help with the following code:

I set the tag = "HC" (Hide controls) for multiple fields on main form and also on controls of page(tabs).  for some reason the current code will hide the pages when I name the control specifically and not with the ctl.tag.

I also need to modify code to be able to hide controls on a subform.  

Please help,

K

Private Sub HideMultiUnitflds()
    Dim ctl As Control
    Dim nfrmSub As SubForm
    Dim nMU As Boolean
    
    nMU = Me.MultiUnit.Value
    
    If nMU = True Then
        For Each ctl In Me.Controls
            If ctl.Tag = "HC" Then
'                Debug.Print ctl.Name
'                If ctl.Visible = True Then
                    Me.pg_RentComps.Visible = False
                    Me.pg_SubjUnits.Visible = False
                    Me.txtValuePerUnit.Visible = False
                    Me.txtUnits.Visible = False
                    ctl.Visible = False
                'Else
                    Me.pg_RentComps.Visible = True
                    Me.pg_SubjUnits.Visible = True
                    ctl.Visible = True
                'End If
            End If
        Next
    End If
End Sub

Open in new window

Karen SchaeferBI ANALYSTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IrogSintaCommented:
One problem with your code is that you make the same controls visible or hidden numerous times, depending on how many controls you have on your form.  I am referring to pg_RentComps, pg_SubjUnits, txtValuePerUnit, and txtUnits.  These should not be inside the loop.

I don't quite get your process.  Your procedure is named HideMultiUnitflds yet it doesn't necessarily hide.  It really toggles the fields to be hidden or visible every time you call it as long as MultiUnit is set to true.  If MultiUnit is false; however, your procedure bypasses the toggling and the fields remain in their same state.  Is this really what you intended?  I just want to be certain.

As for your tab pages not being hidden when you use HC in their tags, it really should have worked so try double checking if you indeed have HC in each individual page.  If you do, then there's something else going on with your form.  You can try creating another form with tabs to see if it exhibits the same issue.  Other than that, you may just have to upload a copy of your db with just that form so that we could have a look at it.

Ron
1
John TsioumprisSoftware & Systems EngineerCommented:
Your Code is a bit complicated given the fact that you just need to hide controls based on the Tag.
Private Sub HideMultiUnitflds()
    Dim ctl As Control
        For Each ctl In Me.Controls
            If ctl.Tag = "HC" Then
'                Debug.Print ctl.Name
                    ctl.Visible = False
                Else
                ' No Action takens since the Tag dictates that we don't want to hide it
                'End If

        Next

End Sub
                       

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karen SchaeferBI ANALYSTAuthor Commented:
thanks.

Here is my final code.

Private Sub HideMultiUnitflds()
    Dim ctl As Control
    Dim nMU As Boolean
    Dim stag As String
   
    ' certain fields are to be visible only if multi-unit is checked
    ' is multi-unit checked?
    nMU = Nz(Me.MultiUnit.Value, False)
       
    For Each ctl In Me.Controls
        stag = ctl.Tag
        If stag = "HC" Then
            ctl.Visible = nMU
        End If
    Next
   
    Me.frm_SalesComps_sub.Form.CompNotes.SetFocus
   
    For Each ctl In Me.frm_SalesComps_sub.Form.Controls
        stag = ctl.Tag
        If stag = "HCSub" Then
            ctl.Visible = nMU
        End If
    Next
End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.