ForeColor Property

Microsoft Access Visual Basic

ForeColor Property

       

You can use the ForeColor property to specify the color for text in a control. Read/write Long.

expression.ForeColor

expression   Required. An expression that returns one of the objects in the Applies To list.

Remarks

You can use this property for controls on forms or reports to make them easy to read or to convey a special meaning. For example, you can change the color of the text in the UnitsInStock control when its value falls below the reorder level.

You can also use this property on reports to create special visual effects when you print with a color printer. When used on a report, this property specifies the printing and drawing color for the Print, Line, and Circle methods.

The ForeColor property contains a numeric expression that represents the value of the text color in the control.

You can use the Color Builder to set this property by clicking the Build button to the right of the property box in the property sheet. Using the Color Builder enables you to define custom colors for text in controls.

For controls, you can set this property by using Font/Fore Color on the Formatting (Form/Report) toolbar, the control's property sheet, a macro, or Visual Basic.

You can set the default for this property by using a control's default control style or the DefaultControl method in Visual Basic.

For reports, you can set the ForeColor property only by using a macro or a Visual Basic event procedure specified in a section's OnPrint event property setting.

For Table objects, you can set this property using Font/Fore Color on the Formatting (Datasheet) toolbar, or in Visual Basic by using the DatasheetForeColor property.

For a text box, combo box, label, or command button that contains a hyperlink, Microsoft Access automatically sets the ForeColor property to the color specified in the Followed Hyperlink Color or Hyperlink Color box on the Hyperlinks/HTML tab of the Options dialog box, available by clicking Options on the Tools menu. If you remove the hyperlink from the control (for example, by changing the ControlSource property of a bound text box to a source that isn't a Hyperlink field), Microsoft Access sets the ForeColor property back to the default control style. For command buttons, the ForeColor property setting takes effect only if the command button contains a caption rather than a picture.

Example

The following example uses the RGB function to set the BorderColor, BackColor, and ForeColor properties depending on the value of the txtPastDue text box. You can also use the QBColor function to set these properties. Putting the following code in the Form_Current( ) event sets the control display characteristics as soon as the user opens a form or moves to a new record.

Sub Form_Current()
    Dim curAmntDue As Currency, lngBlack As Long
    Dim lngRed As Long, lngYellow As Long, lngWhite As Long

    If Not IsNull(Me!txtPastDue.Value) Then
        curAmntDue = Me!txtPastDue.Value
    Else
        Exit Sub
    End If
    lngRed = RGB(255, 0, 0)
    lngBlack = RGB(0, 0, 0)
    lngYellow = RGB(255, 255, 0)
    lngWhite = RGB(255, 255, 255)
    If curAmntDue > 100 Then
        Me!txtPastDue.BorderColor = lngRed
        Me!txtPastDue.ForeColor = lngRed
        Me!txtPastDue.BackColor = lngYellow
    Else
        Me!txtPastDue.BorderColor = lngBlack
        Me!txtPastDue.ForeColor = lngBlack
        Me!txtPastDue.BackColor = lngWhite
    End If
End Sub