making objects visible depending on option box selection

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to make controls on a form visible orinvisible depending on the
selection in an option box.ie if option 1 is selected then only a certain
selection is visible.If option 2 is selected then the section visible for
option 1 is no longer visible and a new section becomes visible.Is this
possible?
 
Steve,

Add the following pseudo-code to the form's Current event:

Me!txtTextBox1.Visible = (Me!optMyOptionBox = True)
Me!txtTextBox2.Visible = (Me!optMyOptionBox = False)
Me!cboMyCombo1.Visible = (Me!optMyOptionBox = True)
Me!cboMyCombo2.Visible = (Me!optMyOptionBox = True)
Me!cmdButton1.Visible = (Me!optMyOptionBox = False)
Me!cmdButton2.Visible = (Me!optMyOptionBox = True)

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
You will also need similar code in the OptionBox's AfterUpdate Event.

Steve,

Add the following pseudo-code to the form's Current event:

Me!txtTextBox1.Visible = (Me!optMyOptionBox = True)
Me!txtTextBox2.Visible = (Me!optMyOptionBox = False)
Me!cboMyCombo1.Visible = (Me!optMyOptionBox = True)
Me!cboMyCombo2.Visible = (Me!optMyOptionBox = True)
Me!cmdButton1.Visible = (Me!optMyOptionBox = False)
Me!cmdButton2.Visible = (Me!optMyOptionBox = True)

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Please respond to the Newsgroup, so that others may benefit from the exchange.
Peter R. Fletcher
 
That's true! Well caught Peter. I was only thinking about navigating from
record to record.

If as Peter suggests, you need this behaviour after changing the OptionBox's
value, put my suggested code into its own private form procedure. Then call
that procedure from the control's AfterUpdate event (and from the form's
Current event).

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
 
Hi Steve,

Another suggestion for you. You could use the tag value of the controls to
specify which group they belong to (i.e. you said some will be visible
whilst others are not). We'll call them Group1 and Group2.

1. Place an option group frame with a couple of option controls (radio
buttons or check boxes, etc, whatever) on your form.
2. For each control that you want to appear in Group 1, place the text
"Group1" (no quotes) in the controls Tag property.
3. In the AfterUpdate event of the option group frame you placed on your
form, place the following:

Private Sub fraYourFrame_AfterUpdate()
Dim ctl As Control
Dim strSelected As String
Dim strNotSelected As String

Select Case fraYourFrame
Case 1 'Where the option with value 1 is for Group1
strSelected = "Group1"
strNotSelected = "Group2"
Case 2 'Where the option with value 2 is for Group2
strSelected = "Group2"
strNotSelected = "Group1"
End Select

For Each ctl In Me.Controls
If ctl.Tag = strSelected Then
ctl.Visible = True
ElseIf ctl.Tag = strNotSelected Then
ctl.Visible = False
End If
Next ctl

End Sub

NB: A control can't be set to not visible if it has the focus so you may
want to keep that in mind.

HTH.

Jamie
 
Back
Top