Avatar of Megin
Megin
 asked on

Setting up conditional fore color in access form

I have a form with a lot of different fields. I want the fore color of all of the items on the form to turn grey when one of the combo boxes is set to "off project. I have tried to do this as a macro, but it isn't working. I would like to do this in vba code. Can someone tell me how?

Thank you!
Microsoft Access

Avatar of undefined
Last Comment
Megin

8/22/2022 - Mon
Rey Obrero (Capricorn1)

using the afterupdate event of the combo box

private sub combo_afterupdate()

if me.combo="off project" then
   me.text1.forecolor=rgb(204, 204,204)

   me.textN.forecolor=rgb(204, 204,204)

else

   me.text1.forecolor=rgb(0, 0,0)

   me.textN.forecolor=rgb(0, 0,0)


end if

end sub
Megin

ASKER
When I use this code, do I left those spots the terms "text1" and "textN" as they are, or am I using the names of the fields on the form? Also, I realize that rgb (204, 204, 204) must be what is making the font grey, but how does it do that?

Thank you!
Rey Obrero (Capricorn1)

you will replace those "text1" and "textN"  with the actual names of the controls..
and add all the other controls from your form..
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Jeffrey Coachman

But lets be clear:
"I have a form with a lot of different fields. I want the fore color of all of the items on the form to turn grey when one of the combo boxes is set to "off project. "

So you still want them to be able to edit the Grey fields?
Or, ...is your real question here how do "Disable" the controls?
(standard "greyed out "controls that you cannot edit)

You can do this with Conditional formatting.
In design view of the form, Select all the controls you want "disabled" (except the combobox obviously)
Then click the Conditional format icon on the ribbon and do this:
Condition1:
Expression Is  [Status]="Off Project"
...then click the "enable" Button


Screenshot and sample db attached
screenBilling.mdb
Rey Obrero (Capricorn1)

<I would like to do this in vba code. >
Jeffrey Coachman

cap,

I know what the OP asked for, so no need for the bold text.
I chose to post anyway, in case they were interested in alternative.

We all know that sometimes a person my think they need VBA, ...but CF may be an option because:
1. It runs faster than VBA
2. If this was a report, most VBA wont run in Report View.

As always, if you have an issue with any post I make, you can email me directly

As long as I have been here, you should know my knowledge level


JeffCoachman
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Megin

ASKER
I don't want to disable, I just want the color of the font to change to grey.

I would do this with the conditional formatting option you are suggesting, but I already have three conditions listed and it won't let me add any more that way.

I want to do this in the code.

Again, when I plug in that code, which appears to me to be something that should work, it doesn't do anything.
Rey Obrero (Capricorn1)

post the codes that you are using
Megin

ASKER
Ooops!

Sorry. I thought I had already posted that code. Here it is:

Private Sub Combo41_AfterUpdate()
If Me.Combo41 = "Off project" Then
    [Me].[First Name].ForeColor = RGB(204, 204, 204)

    [Me].[Last Name].ForeColor = RGB(204, 204, 204)
   
Else

   [Me].[First Name].ForeColor = RGB(0, 0, 0)

   [Me].[Last Name].ForeColor = RGB(0, 0, 0)
     


   
   
   
End If

End Sub
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Rey Obrero (Capricorn1)

what is the rowsource of the combo41 ?  post it here..
Megin

ASKER
"On Project";"Off Project";"Drop-In";"Visitor";"Off Site DB Team";"Corson Office";"1051 Building";"Off Site Staff"
Rey Obrero (Capricorn1)

try this, copy and paste


Private Sub Combo41_AfterUpdate()
If Me.Combo41 = "Off Project" Then
    [Me].[First Name].ForeColor = RGB(204, 204, 204)

    [Me].[Last Name].ForeColor = RGB(204, 204, 204)
   
Else

   [Me].[First Name].ForeColor = RGB(0, 0, 0)

   [Me].[Last Name].ForeColor = RGB(0, 0, 0)
   
End If

End Sub

test the codes by selecting different items from the combo box, observed if the fore color change..
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Megin

ASKER
My error message.It gives me an error message. Run-time error '2465: Staff Information can't find the field 'I' referred to in your expression.

It looks kind of odd, so I am including the picture here.
Rey Obrero (Capricorn1)

click on Debug and see which code will be highlighted,
i don't think it is coming from the code i posted above..
Megin

ASKER
This is what is highlighted:
 

[Me].[First Name].ForeColor = RGB(204, 204, 204)
Your help has saved me hundreds of hours of internet surfing.
fblack61
Rey Obrero (Capricorn1)

check if the name of the control ( textbox ) is really [First Name].

replace it with the correct control name
Megin

ASKER
The name of the text box is "First Name."

I know it isn't a good name for a text box, but I started building this database when I didn't know any better.
ASKER CERTIFIED SOLUTION
Rey Obrero (Capricorn1)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Megin

ASKER
Thank you, thank you, thank you!!!

It worked!!!!!

You are magnificent, Cap!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.