Changing colors

Your VBA code can change the color of objects on forms. Such changes can be handy to use color-coding to call attention to specific items on a form. For example, if a payment is more than 30 days overdue, you might want to choose the amount due to show up in red (to call attention to the value).

The exact color properties available to you depend on the object for which you're writing code. Some common coloring properties include

1 BackColor: Defines the background color of a text box, combo box, or form section

1 BorderColor: Sets the color of the border surrounding a control (as long as that border isn't transparent)

i ForeColor: Sets the color of text in controls that show text, such as a text box, combo box, or label

When writing code to change the color of any property in the preceding list, use the syntax objectName.property = rgbColor where objectName is the name of the object to color, property is one of the properties that accepts a color, and rgbColor is a color defined as a VBA ColorConstant or an expression that defines a color as a mixture of red, green, and blue. ColorConstants are just predefined keywords that specify some of the most basic colors, as shown in Table 6-1.

Table 6-1

Basic Color Constants and RGB Values

Color

ColorConstant

RGB Equivalent

Black

vbBlack

RGB(0,0,0)

Blue

vbBlue

RGB(0,0,255)

Cyan

vbCyan

RGB(0,255,255)

Green

vbGreen

RGB(0,255,0)

Magenta

vbMagenta

RGB(255,0,255)

Red

vbRed

RGB(255,0,0)

White

vbWhite

RGB(255,255,255)

Yellow

vbYellow

RGB(255,255,0)

The RGB() function allows you to express any of millions of colors. You can use the Colors dialog box in Access to determine the correct RGB numbers to use to express any color. In the form's Design view, click the BackColor, BorderColor, or ForeColor property, and then click the Build (ellipsis) button that appears next to the property name and choose More Colors from the menu. The Colors dialog box opens, initially showing just the basic colors. Click the Custom tab to define your own colors.

To see the RGB numbers for a color, first click in the large rainbow-looking area and then choose a brightness level to the right of that. The selected color appears in the top half of the New/Current box, and the RGB numbers for that color appear under the rainbow area. Figure 6-6 shows the basic procedure for finding the three numbers necessary to define a color from the Colors dialog box.

Figure 6-6:

Use the Colors dialog box to determine RGB.

Figure 6-6:

Use the Colors dialog box to determine RGB.

Click a color

RGB values of selected color

Click a color

RGB values of selected color

Selected color

Notice in Figure 6-6 how the selected color is expressed as a mixture of Red (60), Green (222), and Blue (85). The way to express that color by using the RGB function is simply RGB(60,222,85).

Be aware that backgrounds and borders can also be transparent, or not even visible, no matter how you color them. Properties that determine whether an item is transparent or opaque include i BackStyle: When set to 0 (zero), the background is transparent. When set to 1, the background is opaque and can therefore show color.

i BorderStyle: When set to 0 (zero), the border is transparent. When set to 1, the border is opaque and can therefore show color.

In the following simple example, your form contains a control named DueDate that contains the date when a payment is due. As you scroll through records in the table, you want DueDate to appear in red whenever the payment is more than 30 days past due. Because you want the control to change while you're scrolling through records on the form, you could attach the code to the form's On Current event. The code would appear in a class module, as shown in the following example. (The comment above each line of code tells what the line beneath it does.)

Private Sub Form_Current()

'Make control background opaque. DueDate.BackStyle = 1 'Make control background color white. DueDate.BackColor = vbWhite 'Make font color red. DueDate.ForeColor = vbRed Else

'Make control background transparent. DueDate.BackStyle = 0 'Make font color black. DueDate.ForeColor = vbBlack End If End Sub

When working with more than the basic colors, many programmers prefer to define colors in advance by storing them in variables. To use this method, you must first declare the variable or constant as a Long (long integer number) and then use the RGB function to assign a value to the variable. For example, the following Dim statements declare a bunch of color names as variables containing Long Integer data. Lines below the Dim statements assign colors to those names:

'Declare some color names as Long Integer variables.

Dim Beige As Long, Brown As Long, Chartreuse As Long

Dim DarkBlue As Long, DarkGreen As Long

Dim Fuschia As Long, Gold As Long, Gray As Long

Dim HotPink As Long, Dim Lavender As Long, Maroon As Long

Dim Navy As Long, Olive As Long, Orange As Long

Dim Pink As Long, Purple As Long, Salmon As Long

Dim Silver As Long, Teal As Long

'Assign colors to variables as RGB values.

Olive =

= RGB(128, 128, 0)

Orange

= RGB(255, 165, 0)

Pink =

RGB(255, 192, 203)

Purple

= RGB(128, 0, 128)

Salmon

= RGB(241, 128, 114)

Silver

= RGB(192, 192, 192)

Teal =

RGB(0, 192, 192)

After the color name has been assigned a value, you can use it in your code. For example, the following lines set the background color of the form's Detail section to a Salmon color:

Dim Salmon as Long

FormsiFormi.Detail.BackColor = Salmon

For details on creating variables, see Chapter 4.

Was this article helpful?

0 0

Responses

  • fernanda
    How to assign colors to a variable in access vba?
    8 years ago

Post a comment