how to have a checkbox change color and text of a label control?

R

Repent34

I have a form with a yes/no checkbox and it's accompanying label control.
I'd like the checkbox to start out empty, with the label being a red color
and the text "Not Resolved" to display. then when the user is filling in
the form, and clicks in the yes/no box to put in a checkmark, the label will
then turn green and the text "Resolved" shows up in the label control.

I found this code while googling but not sure where it should be plugged
into. New to all this.

Private Sub tickrelease_AfterUpdate()

If Me.tickrelease = True Then
Me.lblrelease.ForeColor = vbGreen
Me.lblrelease.Caption = "Is Resolved"
Else
Me.lblrelease.ForeColor = vbRed
Me.lblrelease.Caption = "Is Not Resolved"
End If

End Sub
 
D

Damon Heron

In design view of the form, select the checkbox, right-click to select
properties (if not already displayed) and select the event tab. Put the code
in the click event of the chkbox, by clicking on the (...) and going to VB
Window.
Before saving the form, set the caption of the label and the forecolor in
the properties menu.
Make sure the chkbox is not set to triple state (in properties)

Damon
 
D

Dale Fye

I prefer:

me.lblRelease.ForeColor = iif(me.tickRelease, vbGreen, vbRed)
me.lblRelease.Caption = iif(me.tickRelease, "Is Resolved", "Is not resolved")

In addition to putting this in the tickRelease_Click event, you should also
put it in the forms Current event. If you don't do this, and open the
record, then the label color and caption will not necessarily reflect the
value that is in the checkbox.

--
HTH
Dale

email address is invalid
Please reply to newsgroup only.
 
R

Repent

I'm not seeing "the tickRelease_Click event". I do see an "On Click" event
when I have the checkbox selected in design mode. When I put it in there,
by selecting event procedure and the ... and plugging that code between the
"Private Sub CanPlay__Click()" and the "End Sub" I get the error of:
"Compile error: Method or data member not found" When I click ok to that
error message I see in the editor that ".tickRelease" is highlighted Blue.
Is that because I'm not in the .tickRelease event? I tried changing the
verbage to "On Click" and the "OnClick" and still received errors.

The name of my label connected to the yes/no checkbox is "CanPlay". I
changed from "Is Resolved" to "CsnPlay", just FYI.

What am I doing wrong?

thanks;
chris
 
D

Dale Fye

Chris,

1. Name the checkbox and the label with similar names. If you want to use
"CanPlay" then I would name them chk_CanPlay and lbl_CanPlay. This makes it
easy to identify which control you are refering to in your code.

2. When you look in the Event tab of the check boxes property window, it
will show as On Click, but when you actually look at the code in the VB
editor, it will read "chk_CanPlay_Click". Assuming the names mentioned
above, the code for that event should read:

Private Sub chk_CanPlay_Click

me.lbl_CanPlay.Caption = iif(me.chk_CanPlay, "Can Play", "Is not
resolved")
me.lbl_CanPlay.ForeColor = iif(me.tickRelease, vbGreen, vbRed)

End Sub

3. If it were me, I don't think I would change the caption of the checkbox.
I will frequently change the caption of a command button (Save/Close) or
something like that, but a checkbox is either True of False, and I would set
the caption and leave it as "Can Play".

You could also have put this code in the checkboxes AfterUpdate event (both
fire and have the same value whenever the checkbox is clicked).

Don't forget to put the code in the forms OnCurrent event as well.

Dale
 
R

Repent

When I apply your code and instructions I get the following error box:

Compile Error:
Method or data member not found

when I click "ok" to that I can see in the VB editor that the line that
states "Private Sub Form_Current () is highlighted yellow
and down in the code the phrase ".tickrelease" is highlighted in blue

The checkbox I'm trying to do this on is bound to a control source in my
table so I created another check box not bound to anything as a test and got
the same results.

You instructions seem clear to me but something is still amis.

chris
 
D

Dale Fye

Sorry, I copied from my earlier post, and forgot to rename the control to
the one you gave me.

Change that line to:

me.lbl_CanPlay.ForeColor = iif(me.chk_CanPlay, vbGreen, vbRed)

It should not mater if the control is bound or not, you just have to use the
correct names of the controls.

Dale
 
R

Repent

if I replace the word forecolor in the code with backcolor? Is that what
you mean? I tried that but got no results. Is that value plugged into
somewhere else?

chris
 
D

Dale Fye

It would depend on what the BackStyle is. You will need to change that to
Normal, instead of Transparent.

Dale
 
R

Repent34

I can get this to work in one project but not another. When setting up
label/form names just like the project that works the label changes colors
like it should if checked but when I try to create a new record, I get the
error that "cannot find a record in the table tblproduct list with key
matching field productID. Can someone point me in the right direction of
what this means?

chris
 

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