Option to change a control from invisible to visible

G

Guest

I want to have a control or series of controls that are only visible when a
certain control option is selected e.g. when a check box is false a series of
controls are invisible but becomes visible when the box is checked true. How
do I do this?
 
G

Guest

I assume this is for a Yes/No field with a deafult of 'No' (False)?
In the CheckBox's AfterUpdate event:

Me.ControlName.Visible = Me.CheckBox

Repeat for each control you want to toggle.

To retain these changes you will need to use the same code in the OnCurrent
event of your form.

When you start to re-use code like this, it is better to create a
user-defined procedure that does this. You then just call that procedure in
any appropriate event.

So, you could create a procedure like this:

Private Sub ToggleControlVisibility
Me.ControlName.Visible = Me.CheckBox
'Repeat for other text boxes
End Sub

Then in the form's OnCurrent and checkbox's AfterUpdate events you just
need the line:
ToggleControlVisibility

Steve
 
G

Guest

SteveM's answer will do the trick. If there are a large number of controls,
one way to reduce the coding is to put a value in the Tag property of the
controls you want to include and loop through the form's controls collection
and make the changes based on whether the control's tag property has the
value:

Private Sub HideControls()
Dim ctl As Control

With Me
For each ctl in .Controls
If .ctl.Tag = "HideMe" Then
.ctl.Visible = .chkHideControls
End If
Next ctl
End With
End Sub
 
D

Dave Eliot

I also had the same question about being/not being visible. I tried the
"Me.ControlName.Visible = Me.CheckBox" and yes, it works just great on
existing records. The problem comes when I want to add a new record. I get
"Run-time error '94': Invalid use of Null" and the line is highlighted in
the Sub Form Current( ). Any ideas how to avoid this?
 
D

Douglas J. Steele

Me.ControlName.Visible = Nz(Me.CheckBox, False)

if you want the control to be invisible, or

Me.ControlName.Visible = Nz(Me.CheckBox, True)

if you want it to be visible.
 
D

Dave Eliot

That did the trick. Thanks so much!

Douglas J. Steele said:
Me.ControlName.Visible = Nz(Me.CheckBox, False)

if you want the control to be invisible, or

Me.ControlName.Visible = Nz(Me.CheckBox, True)

if you want it to be visible.
 

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

Top