Color with code?

medusa3604

New member
Local time
Today, 12:30
Joined
Jan 30, 2013
Messages
2
Hello,
This is my first project in Access 2010 and youtube been working preety good so far.
I started on a web project that maybe was a big misstake due to no conditional formating.
The project is based on the issues web template.
My queston is, is it possible to get a text box to change color depending on what value it has?
I have a text box with two choices. Import and Export.
If I choose import I want the box to be green and export to be red.
Maybe it´s possible to write this with the VBA editor? The problem is that I dont even know how to find my text box in the editor :(
If anyone have any advice it´s greatly welcome.
Anders
Sweden
 
I have a text box with two choices. Import and Export.
First, I realize that you are quite new to Access but text boxes do not have "choices" they only allow users to type in a value. I suspect that you are really talking about a combo box with two choices.

If this is the case, you can have the background color to be changed based on the value selected in the combo box.

With your form in design view, select the combo box. Be sure the Properties pane is displayed.

First, be sure that you have provided a name for your combo box. Not providing a custom name that you recogonize is a common mistake that many new Access users make. The name is provided by clicking on the Other tab in the Properties pane and high light the default value provided by Access in the Name property field. Type in a name. I would suggest using "cbo" as the prefix and then some descriptive name. It is a really good idea to not use spaces in the new name but use something called Camel case where each word in the name is capitalized. In your case you could name the combo box: cboStatus

Next, click on the Event tab in the Properties pane. Locate the After Update event and click in the blank area. A down arrow and a small command button with 3 dots will appear at the end of the blank area. Click the down arrow and select "[Event Proceedure]" from the list. Once this is selected, click the button with the three dots. You will be taken to the VBA editor and be placed in the After Update event of your combo box.

Copy and paste the code below in to the area just below the
"Private Sub Form_AfterUpdate()" statement

Code:
If Me.NameOfYourComboBox - "Import" Then
     'make the background Green
     Me.NameOfYourComboBox .BackColor = RGB(0, 126, 0)
Else
     'make the background Red
     Me.NameOfYourComboBox .BackColor = RGB(255, 0, 0)
End If

You will need to replace the "NameOfYourComboBox" in the code above with the actual name you have provided for your combo box.

This should cause the action you are wanting.
 
Hello,

Thank you for a most detailed answer.

Yes for sure I am new to this and I´m not sure I´m starting from the right direction.

I am not able to see Design view in my form. Maybe because its a web template?

Anyhow I successfully tried it in another "non" web based form.

Is there a solution to this?

Thanks again for your time.

Anders
Sweden
 
I must admit that I am not up to speed with "web templates" and using them. However, with that said, I have developed forms in a web database and there are "events" that are triggered in forms in web databases just as there are in the normal Access form.

With this in mind, instead of using VBA code, you will have to use macors. VBA code is not allowed in web databases.

Check into creating a macro to do what you want and have an event call that macro.
 

Users who are viewing this thread

Back
Top Bottom