color syntax

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

color syntax

Post by dasadler »

I have a label in an Access 2007 form and I am trying to change the background color based on the value of a checkbox. My code is below and, although I don't receive any errors, it doesn't work. What am I doing wrong?

Code: Select all

    Private Sub NoGoColor()
        If Me.NoGo = True Then
            Me.Label112.BackColor = CF7B79
        Else
            Me.Label112.BackColor = FFFFFF
        End If
    End Sub
Don

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: color syntax

Post by HansV »

Check the BackStyle property of the label. If it's set to Transparent (the default), the BackColor property will be ignored. To see the BackColor, set BackStyle to Normal.
If you want to do this in VBA:

Me.Label112.BackStyle = 1

(0=Transparent, 1=Normal)
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: color syntax

Post by dasadler »

I changed the attribute to normal and still did not work. Then, I changed my color to &HCF7B79 and that made no difference. Oh, I changed the backstyle in the properties sheet, not in VBA - would that matter?
Don

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: color syntax

Post by HansV »

Changing the property in the Property Sheet should be sufficient.
Could you create a copy of the database with just the items needed to demonstrate the problem? Remove everything not relevant to the problem, and all sensitive/proprietary information, then zip the copy and attach it to a reply.
Thanks in advance.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: color syntax

Post by dasadler »

here it is... you may have a link error on opening it... the result of trying to sanitize it - disregard that error.
You do not have the required permissions to view the files attached to this post.
Don

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: color syntax

Post by HansV »

1) You don't call NoGoColor anywhere, so it will never run.

2) You should use &H before hexadecimal values:

Code: Select all

Private Sub NoGoColor()
    If Me.NoGo = True Then
        Me.Label112.BackColor = &HCF7B79
    Else
        Me.Label112.BackColor = &HFFFFFF
    End If
End Sub
This error was not detected because you don't have Option Explicit at the top of the module. Please read The importance of 'Option Explicit'.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: color syntax

Post by dasadler »

where/how would I call this?

In fact, how can I find out how I call any other code I have? I guess I forgot how it was done initially.
Don

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: color syntax

Post by HansV »

When would you like to run NoGoColor?

You can search for a word such as NoGoColor by selecting Edit | Find... in the Visual Basic Editor. You can specify that you want to search the entire project (i.e. all code in the database):
S0073.png
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: color syntax

Post by dasadler »

Okay, so I chose to call it as an on click event procedure and added option explicit and used the following code:

Code: Select all

Private Sub NoGo_Click()
Option Explicit
        If Me.NoGo = True Then
            Me.Label112.BackColor = &HCF7B79
        Else
            Me.Label112.BackColor = &HFFFFFF
        End If
End Sub
Here's the msg I received
You do not have the required permissions to view the files attached to this post.
Don

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: color syntax

Post by HansV »

If you read "The importance of 'Option Explicit'" carefully, you'll see that you shouldn't insert this line inside a procedure (sub) but at the top of the module, above all procedures and functions.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: color syntax

Post by dasadler »

Hmmm, I though I had it. Now I am not sure where to put it. Seems like I recall a lot of code from you that had Option explicit but cannot seem to find any of it now. I was sure you had it in there somewhere. I already have one option at the top... is that where it goes? pardon the spelling in the image.
You do not have the required permissions to view the files attached to this post.
Don

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: color syntax

Post by HansV »

You can insert the line Option Explicit immediately above or immediately below the line Option Compare Database. As long as it is above the first Sub or Function in the module.
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: color syntax

Post by dasadler »

Okay it works now... sort of. I set it up as an onclick event and it seems to work as designed but not what I need.

If the NoGo check box is already checked, then the back ground color does not appear unless I click it off then click it back on (uncheck then re check). I think I have the wrong trigger event.

If the box is already checked then the background should already be in a different color when I come to the record and if it is not already checked then the background should be white until it is checked.

How would I set that up?
Don

User avatar
HansV
Administrator
Posts: 78642
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: color syntax

Post by HansV »

You also need to create an event procedure for the On Current event of the form. This will set the background color when the form is opened, and when you move from record to record:

Code: Select all

Private Sub Form_Current()
    If Me.NoGo = True Then
        Me.Label112.BackColor = &HCF7B79
    Else
        Me.Label112.BackColor = &HFFFFFF
    End If
End Sub
Best wishes,
Hans

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: color syntax

Post by dasadler »

Wonderful. Thank you very much.
Don