option group background color (1 Viewer)

Siegfried

Registered User.
Local time
Today, 20:20
Joined
Sep 11, 2014
Messages
105
Dear experts,

I'm trying to figure out how to change the OptionGroup background field to color based on the value of the option button.
I found that the control is transparant: OptionGroup.BackStyle 0 and can be changed to normal OptionGroup.BackStyle 1 and then the interior color can be set by the BackColor property.

I'd like the OptionGroup BackColor to be green on Yes/True and red on No/False.

Can someone please help me with this code?

Thanks

Best regards,

Siegfried
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:20
Joined
May 7, 2009
Messages
19,242
add code to the OptionGoup AfterUpdate.
if the first Option is Yes:


private sub OptionGroupName_AfterUpdate()
With Me.OptionGroupName
If .Value=1 'yes
.backcolor=vbGreen
Else
.backcolor=vbred
end if
end with
end sub
 

Siegfried

Registered User.
Local time
Today, 20:20
Joined
Sep 11, 2014
Messages
105
add code to the OptionGoup AfterUpdate.
if the first Option is Yes:


private sub OptionGroupName_AfterUpdate()
With Me.OptionGroupName
If .Value=1 'yes
.backcolor=vbGreen
Else
.backcolor=vbred
end if
end with
end sub

Thanks, tried but not getting a color, also when I switch the option button I get error 2427: "your entered an expression that has no value.

Code:
Private Sub Frame28_AfterUpdate()

With Me.Frame28
If Option31.Value = 1 Then
Frame28.BackColor = vbGreen
Else
Frame28.BackColor = vbRed
End If
End With
End Sub
 

June7

AWF VIP
Local time
Today, 10:20
Joined
Mar 9, 2014
Messages
5,470
Did you set the BackStyle property to Normal?

Don't reference Option31, the frame has the value, not the button:

If .Value = 1 Then

If you use With block for Frame28, no need to repeat Frame28 qualifier.

Code works for me.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:20
Joined
May 7, 2009
Messages
19,242
Private Sub Frame28_AfterUpdate()

With Me.Frame28
.BackColor=vbWhite
If .Value = 1 Then
.BackColor = vbGreen
Else
.BackColor = vbRed
End If
End With
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:20
Joined
May 7, 2009
Messages
19,242
we are testing Frame28, not the individual Option31.
 

Siegfried

Registered User.
Local time
Today, 20:20
Joined
Sep 11, 2014
Messages
105
we are testing Frame28, not the individual Option31.

I got confused.
I entered the code as you showed, I can toggle the buttons but no color, option group background stays transparent?

Code:
Private Sub Frame28_AfterUpdate()

With Me.Frame28
.BackColor = vbWhite
If .Value = 1 Then
.BackColor = vbGreen
Else
.BackColor = vbRed
End If
End With
End Sub
 

June7

AWF VIP
Local time
Today, 10:20
Joined
Mar 9, 2014
Messages
5,470
See post4: Did you set the BackStyle property to Normal?
 

Siegfried

Registered User.
Local time
Today, 20:20
Joined
Sep 11, 2014
Messages
105
See post4: Did you set the BackStyle property to Normal?

Thanks, overlooked that.
I'm almost there, but when I close the form the colors go back to default transparant, how can I make that it stays green when yes and red when no?


Code:
Private Sub Frame28_AfterUpdate()

With Me.Frame28
.BackStyle = 1
.BackColor = vbGreen
If .Value = -1 Then
.BackColor = vbGreen
Else
.BackColor = vbRed
End If
End With
End Sub
 

isladogs

MVP / VIP
Local time
Today, 19:20
Joined
Jan 14, 2017
Messages
18,219
Just set the background as normal in the form design.
No code needed
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:20
Joined
May 7, 2009
Messages
19,242
call it also on the Current event of yhe form:

private sub form_current()
call Frame28_AfterUpdate
end sub

Private Sub Frame28_AfterUpdate()

With Me.Frame28
.BackStyle = 1
.BackColor = vbWhite
If .Value = -1 Then
.BackColor = vbGreen
Elseif .value=2 then
.BackColor = vbRed
End If
End With
End Sub
 

Siegfried

Registered User.
Local time
Today, 20:20
Joined
Sep 11, 2014
Messages
105
I got on more last question.
When I select for a new record in form only one Option Group show a button and the right color, my other 3 Option Groups don't show buttons and all have color red value false?
How can I fix that?

Thanks.
 

June7

AWF VIP
Local time
Today, 10:20
Joined
Mar 9, 2014
Messages
5,470
Don't understand "don't show buttons". How can option group not show buttons?

What color do you want the background to show?

Perhaps you need to set Default Value of the frame.
 

Siegfried

Registered User.
Local time
Today, 20:20
Joined
Sep 11, 2014
Messages
105
Hi June,

Thanks again for your interest in my post/question.
I have uploaded a picture, you will understand when you see.
This is what I get after pressing my New button for adding a new record.
 

Attachments

  • Capture.JPG
    Capture.JPG
    84 KB · Views: 161

Siegfried

Registered User.
Local time
Today, 20:20
Joined
Sep 11, 2014
Messages
105
I figured it out, thank to your remark on the default values.
They were incorrect, now all working.

Thanks a lot!!!
 

Users who are viewing this thread

Top Bottom