How to enable group of controls by option group selected?

J

Jon

I have a form contains 1 option group which has 3 controls A,B & C. and I
have 3 group of controls(textbox, checkbox,combobox…) each group is belong to
one of the above controls in the option group. What I want to do is if the
select one of the option group, say A, the controls belongs to it will be
enabled and others disabled and so on. Please advice??
 
B

BruceM

I'm unclear on what you mean when you refer to a text box, check box, and
combo box being part of an option group. An option group is for making a
single selection from among several choices. For example, age range: 18 -
30, 31 - 50, 51 - 65, 65+. This can be accomplished with radio buttons,
toggle buttons, or check boxes.

If you are referring to a group of controls, in Access 2003 and earlier you
can only work with each control individually. It may be different in Access
2007, but I don't know for sure one way or the other.

You could have code like this in the After Update event for the combo box:

Me.CheckBox1.Enabled = (Me.ComboBox1 = "Yes")
Me.TextBox1.Enabled = (Me.ComboBox1 = "Yes")

or:

Dim blnEn as Boolean

blnEn = (Me.ComboBox1 = "Yes")

Me.CheckBox1.Enabled = blnEn
Me.TextBox1.Enabled = blnEn

You would need the same code in the form's Current event.
 
J

Jon

Hi BuceM,

What I mean is I have 1 option group which has 3(A,B,C) radio buttons. When
the user select for example A, the controls txt1, cbo1 & checkbox1 will be
enabled and txt2, txt3, cbo1, cbo2, checkbox3 will be disabled until the user
select B, or C and the same action will be applied on txt2…..
Thanks
 
B

BruceM

Each of the three option group radio buttons has a value you can view in the
button's properties. The option group itself has the value of the selected
button. If the buttons A, B, and C have the values 1, 2, and 3
respectively, choosing the A button will mean the option group's value is 1.
Therefore your code is something like this (grp1 is the name of the option
group):

Me.txt1.Enabled = (Me.grp1 = 1)
Me.chk1.Enabled = (Me.grp1 = 1)

If grp1 = 1, the statement Me.grp1 = 1 is True; therefore the statement
becomes:
Me.txt1.Enabled = True

As I mentioned, this code goes in the After Update event for grp1 and in the
forms's Current event.
 
J

Jon

Thank you BruceM
But I am wondering if we could use the “TAG†property instead of controls
names due to too many controls in my form
 
B

BruceM

How many controls are you talking about here?

You could set the Tag property of the necessary controls to, say, "E" (for
Enable, but it doesn't matter what you use). Then, in the form's code
module you can create a function:

Private Function EnableDisable()

Dim ctl as Control

For Each ctl in Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acCheckBox
ctl.Enabled = (ctl.Tag = "E")
End Select
Next ctl

End Function

In the After Update event for the option group and in the form Current
event, call the function by placing EnableDisable on its own line of code.
You could use the Call syntax to make it easier to read:
Call EnableDisable

You need to test the type of control as shown, as some controls do not have
an Enabled property, and you will get an error.
 

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