Best way to Hide

  • Thread starter Charles D Clayton Jr
  • Start date
C

Charles D Clayton Jr

I have a form where, do to space reasons, I need to hide and unhide
multiple fields. Presently I am able to accomplish this with code
like this:
Me.lblBadge.Visible = False
Me.lblDateIssued.Visible = False
Me.txtBadge.Visible = False
Me.txtDateIssued.Visible = False

I was wondering if there was a more efficient way to do multiple
controls at one time? I am hiding and unhiding around 20 controls at a
time. Since I am picking up coding in such a hodge-podge manner, I do
not know if there is a better way to accomplish this task or perhaps a
more professional one.

Thanks,

Charles D Clayton Jr
 
J

Joel Wisehart

Use the "With" structure:

With Me
.lblBadge.Visible = False
.lblDateIssued.Visible = False
.txtBadge.Visible = False
.txtDateIssued.Visible = False
End With

This works for other objects too:

With Me.lblBadge
.Forecolor = vbRed
.Enabled = False
.Locked = True
End With
 
J

Joel Wiseheart

One other thing is that if you are doing the same
operation to all of the fields, like searching for null
values to detect any fields left blank, you can also use
a "For Each" command, to loop through all of the controls
on a form:

Dim ctl as Control

For Each ctl in Me!Controls
If ctl.Value = Null Then
....Do something here....
End if
Next

There's a much more thourough explanation of "With"
and "For Each" structures in the Access Help files.
Check 'em out!

Hope this helps
Joel
 
D

Dirk Goldgar

Joel Wiseheart said:
One other thing is that if you are doing the same
operation to all of the fields, like searching for null
values to detect any fields left blank, you can also use
a "For Each" command, to loop through all of the controls
on a form:

Dim ctl as Control

For Each ctl in Me!Controls
If ctl.Value = Null Then
....Do something here....
End if
Next

There's a much more thourough explanation of "With"
and "For Each" structures in the Access Help files.
Check 'em out!

Extending the above idea, you can use the Tag property of the controls
to organize them into groups -- giving all the controls to hidden or
shown as a group the same tag -- and use a function like this to show or
hide them:

'----- start of function code -----
Function ShowControlGroup( _
OnForm As Form, _
GroupName As String, _
Optional ShowOrHide As Boolean = True)

' Shows or hides a group of controls, as identified by the
' presence of <GroupName> in their Tag properties.
'
' Arguments:
' OnForm - A reference to the form object containing
' the controls
' GroupName - The "name" of the group; a string to look
' for in each control's Tag property
' ShowOrHide - True (the default) to show the controls;
' False to hide them.

On Error GoTo Err_ShowControlGroup

Dim ctl As Access.Control

Const conERR_CANT_HIDE = 2165
' Error number if control to be hidden has the focus

For Each ctl In OnForm.Controls
If ctl.Tag Like "*" & GroupName & "*" Then
ctl.Visible = ShowOrHide
End If
Next ctl

Exit_ShowControlGroup:
Exit Function

Err_ShowControlGroup:
If Err.Number = conERR_CANT_HIDE Then
' If we can't hide this control because it has the
' focus, just skip it.
Resume Next
Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
Resume Exit_ShowControlGroup
End If

End Function

'----- end of function code -----

From code behind the form, you could hide all the controls you've tagged
as "Group1" using this line of code:

ShowControlGroup Me, "Group1", False

and show them again using:

ShowControlGroup Me, "Group1", True
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top