简体   繁体   中英

Color field in access 2003

On MS-Access 2003 i've a mask that shows results of a query. For example result of query is:

Column1 Column2

1 Y

2 N

3 N

4 Y

It shows in the mask ad a table. I need to color background field of column2 if the value is Y. To do that i've use the code:

Private Sub Form_Current()
    if (Column2) = "Y" Then
        Stato.BackColor = vbGreen
    End If
End Sub

But it colored all background. So i've tried a workaround:

For Each ctl In Me.Section(acDetail).Controls
    If (ctl) = Column2 Then
        If (Me.Column2) = "Y" Then
          ctl.BackColor = QBColor(2)
        End If
    End If

But this also colored all bg. Some suggestion?

You can add conditional formatting in code using something like this. This function is based on some code I've used and you may need to tweek it to fit your specific requirements.

Dim fcd As FormatCondition
Dim ctl As control
Dim frm As Form
Dim txt As TextBox
Dim strCond As String

For Each ctl In frm.Controls
    If TypeOf ctl Is Access.TextBox Then
        If ctl.Visible = True Then
            Set txt = ctl
            If txt.Name = "Column2" Then
                strCond = "=Y"
                Set fcd = txt.FormatConditions.Add(acExpression, acEqual, strCond)
                fcd.BackColor = QBColor(2)
            End If
        End If
    End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM