Radio Button Problem

J

Jen

Hi again guys,
I've got a form which, among other controls, has two radio buttons
beside each other with the labels 'Active' and 'Inactive'. This relates to
whether a member of staff is currently working on a particular job or not. My
problem is that when i click in the 'Active' control, i'd like the label to
turn green and when i click in the 'Inactive' control, i'd like that label to
turn red and the other label to revert back to plain white.
I'd also only like one 'dot' to be visible at all times. Is this possible
??

Hope i've explained it ok

Thanks guys,
Jen
 
B

BruceM via AccessMonster.com

This is where an Option Group comes into play. Create an option group using
the toolbox. Add option buttons inside the option group box. You may need
to create new ones, as I think option buttons created outside an option group
have different properties than ones created inside the option group. Assign
each button an Option Value. Use 1 and 2. It doesn't matter which is which,
but for this discussion active is 1 and Inactive is 2.

Bind the option group to a number field ActiveStatus. You can set the
default value of the option group to 0, which should leave you with white
buttons rather than gray until one of the choices is selected. Or set the
Default Value to 1 to mark the Active button by default for a new record.

You no longer have an Active field and an Inactive field, but a single
ActiveStatus field that will be 1 for Active and 0 for Inactive. Remember,
the Option Group is bound to the field. The buttons are not bound to any
field.

I will call the Option Group grpActive, and the Labels lblActive and
lblInactive. In the After Update event of grpActive:

If Me.grpActive = 1 Then
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
ElseIf Me.grpActive = 2 Then
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbRed
Else
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
End If

You could also use Select Case

Select Case Me.grpActive
Case 1
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
Case 2
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbRed
Case Else
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbWhite
End If

You can simplify the expression if there are just two options:

If Me.grpActive = 1 Then
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
Else
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbRed
End If

You will need the same code in the form's Current event so the labels display
correctly when you arrive at an existing record.
 
A

Al Campagna

Jen
Sounds like your Active/Inactive logic should be Boolean (True/False)
Two individual, or two "Option Grouped" radio buttons are unnecessary.
Ex. A field in your table named Status (Boolean True/False)
One check box (or radio button) on the form will be able to indicate
both conditions. This presents a cleaner interface, and reduces coding
needed.
Ex, True display...
Status: O Inactive
or
Ex. False display...
Status: X Active

Using the AfterUpdate event of Status...

Private Sub Status_AfterUpdate()
If Status = True Then
lblStatus.ForeColor = QBColor(10)
lblStatus.Caption = "Active"
Else
lblStatus.ForeColor = QBColor(12)
lblStatus.Caption = "InActive"
End If
End Sub

That will change the label and label color whenever Status is changed.

**You'll also need that same code in the OnCurrent event of the form, so
that when you browse to a record, the Status label and forecolor will be
corrected for it's value.
--
hth
Al Campagna
Microsoft Access MVP
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love... and you'll never work a day in your life."
 
D

Daryl S

Jen -

Put this code in the option group Click event (change all occurrences of
opActiveInactive below to the name of your option group, and if necessary,
change the values of the active and inactive options).

Private Sub opActiveInactive_Click()
' If Active is chosen, turn that label green. If Inactive, turn that lavel
red
If Me.opActiveInactive.Value = 1 Then
Me.lblActive.ForeColor = vbGreen
Me.lblInactive.ForeColor = vbBlack
Else
Me.lblActive.ForeColor = vbBlack
Me.lblInactive.ForeColor = vbRed
End If
End Sub

You will also need to call this from the Load or Current form events so it
is set initially.

As for one radio button always selected, set the default to either Active or
Inactive, and that will take care of it.
 
B

BruceM via AccessMonster.com

I think the OP had free-standing option buttons, not an option group. In any
case, this seems to be essentially what I suggested. However, I would not
use the Click event, as it would run every time somebody clicks anywhere in
the option group. The After Update event is preferable, as it runs only when
the option group is updated.

The code needs to be in the Current event to assure correct label colors
every time the user navigates to a record. The Load event would affect only
the first record displayed.

AFAIK an option value needs to be numeric. The default value could be 1 to
show Active, but it cannot be set directly to "Active".

Daryl said:
Jen -

Put this code in the option group Click event (change all occurrences of
opActiveInactive below to the name of your option group, and if necessary,
change the values of the active and inactive options).

Private Sub opActiveInactive_Click()
' If Active is chosen, turn that label green. If Inactive, turn that lavel
red
If Me.opActiveInactive.Value = 1 Then
Me.lblActive.ForeColor = vbGreen
Me.lblInactive.ForeColor = vbBlack
Else
Me.lblActive.ForeColor = vbBlack
Me.lblInactive.ForeColor = vbRed
End If
End Sub

You will also need to call this from the Load or Current form events so it
is set initially.

As for one radio button always selected, set the default to either Active or
Inactive, and that will take care of it.
Hi again guys,
I've got a form which, among other controls, has two radio buttons
[quoted text clipped - 10 lines]
Thanks guys,
Jen
 
J

Jen

Hi Daryl,
Thanks for your help....i've put the code in the Click event of the
option group but i'm not sure what you mean by 'call from the Load or Current
form events'. Does this mean i also must put the same code in the Load event
of the form itself ? I'm no expert so sorry if this is a silly question

Thanks,
Jen
 
B

BruceM via AccessMonster.com

It should be in the After Update event, not the Click event, and it should be
in the form's Current event. The Load event will not work. Reasons are
described in my previous posting.

Open the form in design view. Click View >> Properties if the Property Sheet
is not already open. Click the Event tab. Click the three dots next to On
Current. It may open the code window directly, or you may need to click Code
Builder, OK. Seems to me this changed fairly recently. In any case, place
the code between Private Sub Form_Current() and End Sub.
Hi Daryl,
Thanks for your help....i've put the code in the Click event of the
option group but i'm not sure what you mean by 'call from the Load or Current
form events'. Does this mean i also must put the same code in the Load event
of the form itself ? I'm no expert so sorry if this is a silly question

Thanks,
Jen
[quoted text clipped - 34 lines]
 
A

Al Campagna

Jen,
First, a minor point...
The Click event of an option group fires every time
you click within the area of the option group, not just when changing
(updating)
the value. The more appropriate event would be the AfterUpdate event of the
option box. It only fires when the option group value changes.
Not a serious issue, but it should be mentioned.

The form's Load event only fires once... when the form is initially
opened.
You need your code to run every time you access a particular record. (so
the
correct forecolors are applied to the option group labels)
Therefore, you need to place the same "color" code as the AfterUpdate...
in
the form's OnCurrent event.

While there is no "rule", and there's nothing "wrong", with using a 2
check box
option group to represent a Boolean value, but just a simple check box, with
changing labels would be more appropriate.
Option groups, by their nature, are not Boolean, their purpose is to
return an
integer value. While a two check option group can be made to handle Boolean
logic, it is not really intended to do that.

Continue on with your option group solution... but consider my
suggestion for
your next Boolean field...
--
hth
Al Campagna
Microsoft Access MVP
http://home.comcast.net/~cccsolutions/index.html

"Find a job that you love... and you'll never work a day in your life."
 
B

BruceM

Jen,

There have been a number of replies in this thread that are not showing up
here. I used AccessMonster, and Al Campagna made some replies too. If you
are using the Microsoft web interface you are not getting all of the
conversation. The web interface has often had problems, and it seems
unlikely it will ever be reliable. Maybe the problem is somewhere other than
the Microsoft web page, but I doubt it. You should use a different news
reader. For web interfaces AccessMonster is not too bad, but best would be a
newsreader. Most e-mail programs can be used as newsreaders, or you can use
a dedicated newsreader program.

In any case, the Click event will fire when you click anywhere within the
option group that is not already occupied by another control. The best event
to use is After Update.

Also, the Load event will apply only to the first record you see upon
loading the form. The formatting applied then will remain until the next
time you update the option group (or click it, if you decide to go that way).
It will not change when you move to another record. To do that you need to
use the form's Current event.

You can call the option group After Update event in the form's Current event
or vice versa, but since either event could have code beyond what applies to
formatting, you may do better to create your own function. In the VBA editor
click Insert >> Procedure. Choose Function and Private, and give it a name
such as ControlColor. You can make the function Public, but if it is to be
used only for the one form there is no need to do that.

Insert the formatting code between Private Function ControlColor() and End
Function. In the form's Current event or the option group After Update event:

Call ControlColor

Here is the reply I posted last week:

***********
This is where an Option Group comes into play. Create an option group using
the toolbox. Add option buttons inside the option group box. You may need
to create new ones, as I think option buttons created outside an option group
have different properties than ones created inside the option group. Assign
each button an Option Value. Use 1 and 2. It doesn't matter which is which,
but for this discussion active is 1 and Inactive is 2.

Bind the option group to a number field ActiveStatus. You can set the
default value of the option group to 0, which should leave you with white
buttons rather than gray until one of the choices is selected. Or set the
Default Value to 1 to mark the Active button by default for a new record.

You no longer have an Active field and an Inactive field, but a single
ActiveStatus field that will be 1 for Active and 0 for Inactive. Remember,
the Option Group is bound to the field. The buttons are not bound to any
field.

I will call the Option Group grpActive, and the Labels lblActive and
lblInactive. In the After Update event of grpActive:

If Me.grpActive = 1 Then
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
ElseIf Me.grpActive = 2 Then
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbRed
Else
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
End If

You could also use Select Case

Select Case Me.grpActive
Case 1
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
Case 2
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbRed
Case Else
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbWhite
End If

You can simplify the expression if there are just two options:

If Me.grpActive = 1 Then
Me.lblActive.BackColor = vbGreen
Me.lblInactive.BackColor = vbWhite
Else
Me.lblActive.BackColor = vbWhite
Me.lblInactive.BackColor = vbRed
End If

You will need the same code in the form's Current event so the labels display
correctly when you arrive at an existing record.
*****************

Al suggested that for Active or Inactive there are just two choices, so a
single check box may be preferable to an Option Group. It would certainly be
simpler.

You can see the full discusssion at www.accessmonster.com
 
J

Jen

Thats it.....working perfectly now !. Thank you for taking the time to help
me Daryl i certainly appreciate it. Bruce, cheers for the advice regarding
the newsreader, i'll definitely look into it. Also, please relay my thanks to
anyone who took the time to answer but whose post didn't appear in the thread.

You guys are great.
Jen xoxo
 

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