Option Group

G

Guest

I have an option group of 4 different choices (Source Report, Destination
Report, Special Report, Origin Report)

The values set for these are 0,1,2,3

On my form I have labels and dropdown boxes that I want to appear when a
certain choice is selected. Right now I have the visible feature on all of
these set to false. Where do I go to say "if Source report option selected
then make labelX.visible = true"

Thanks
 
D

Dirk Goldgar

James C. said:
I have an option group of 4 different choices (Source Report,
Destination Report, Special Report, Origin Report)

The values set for these are 0,1,2,3

On my form I have labels and dropdown boxes that I want to appear
when a certain choice is selected. Right now I have the visible
feature on all of these set to false. Where do I go to say "if Source
report option selected then make labelX.visible = true"

Use the AfterUdate event of the option group frame. You don't give the
names of the controls involved, but the code might look something like
this:

'----- start of example code -----
Private Sub optReportType_AfterUpdate()

Select Case Me!optReportType
Case 0
Me!lblSourceRpt.Visible = True
Me!lblDestRpt.Visible = False
Me!lblSpecialRpt.Visible = False
Me!lblOriginRpt.Visible = False
Case 1
Me!lblSourceRpt.Visible = False
Me!lblDestRpt.Visible = True
Me!lblSpecialRpt.Visible = False
Me!lblOriginRpt.Visible = False
Case 2
Me!lblSourceRpt.Visible = False
Me!lblDestRpt.Visible = False
Me!lblSpecialRpt.Visible = True
Me!lblOriginRpt.Visible = False
Case 3
Me!lblSourceRpt.Visible = False
Me!lblDestRpt.Visible = False
Me!lblSpecialRpt.Visible = False
Me!lblOriginRpt.Visible = True
Case Else
Me!lblSourceRpt.Visible = False
Me!lblDestRpt.Visible = False
Me!lblSpecialRpt.Visible = False
Me!lblOriginRpt.Visible = False
End Select

End Sub
'----- end of example code -----

If the option group is a bound control, then you'll also want to call
its AfterUdate event procedure from the form's Current event, so that
the other control are shown or hidden appropriately as you navigate from
record to record.

If you have whole sets of controls that should be visible or hidden
based on this option group, you may find it more efficient to group them
together somehow -- maybe using their Tag properties, or with an array
of control names -- so that you can loop through controls without having
to name each one in a separate code line.

Note also that you can't hide a control that has the focus. Therefore,
if any of the controls might have the focus, you must set the focus
elsewhere before hiding it.
 
G

Guest

Thanks for the help.

One last question. How does the Tag property work? There are enough controls
that I don't want to list them one by one. I would rather link them up such
as tagSource...so that I could just make tagSource.visible = true...and so
on. Anyways, do you know how the Tag property works and how to call it?

Thanks
 
D

Dirk Goldgar

James C. said:
Thanks for the help.

One last question. How does the Tag property work? There are enough
controls that I don't want to list them one by one. I would rather
link them up such as tagSource...so that I could just make
tagSource.visible = true...and so on. Anyways, do you know how the
Tag property works and how to call it?

The Tag property is available for all controls, and you can set it to
anything you want. So you can group controls by setting all of their
Tag properties to the same value. For example, suppose you had some
text boxes and labels are mutually exclusive by option type; so that
one set is supposed to be visible only when the option group is set for
"Source", others are to be visible only when the option is
"Destination", others only when it's "Special", and others only when
it's "Origin". I'm going to suppose also -- to simplify the code --
that you won't use the Tag property for anything else, so that any
control on this form that has a Tag property should be shown only when
the appropriate report type is selected in the option group.

With the form open in design view, select all the controls in the
"source report" group, bring up their joint property sheet, and set the
Tag property (on the Other tab of the property sheet) to "0" (without
the quotes). Then select the "destination report" controls, bring up
their joint property sheet, and set their Tag property to "1". Set the
Tag property for the "special report" controls to "2", and the "origin
report" controls to "3". This way, the Tag property corresponds to the
value of the option group.

Now use code like this in your option group's AfterUpdate event:

'----- start of example code -----
Private Sub optReportType_AfterUpdate()

Dim ctl As Access.Control
Dim strRptType As String

strRptType = CStr(Me!optReportType)

For Each ctl In Me.Controls
If Len(ctl.Tag) > 0 Then
ctl.Visible = (ctl.Tag = strRptType)
End If
Next ctl

End Sub
'----- end of example code -----

If you have a more complicated requirement, where some controls should
be shown for two or more option values, this logic will need to be
modified.
 
M

Mark M

In the form design view, each control has a Tag property (I believe it's on
the "other" tab of the property window). Just add whatever text you want in
the tag property, like "hide". Then you can loop through the controls
collection of the form in the current event or after a certain criteria is
met. Something like the following air code:

Dim ctl as Control
For Each ctl in Me.Controls
If ctl.Tag = "hide" Then
ctl.Visible = False
End If
Next ctl

Of course you can add logic to make them all visible again, or whatever you
want.
 

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