Reputation: 21
On MS-Access 2003 i've a mask that shows results of a query. For example result of query is:
Column1Column2
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?
Upvotes: 0
Views: 134
Reputation: 970
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
Upvotes: 1