J.M*_*J.M 1 ms-access vba userform
使用基于 Access 的应用程序。需要根据存储在这些字段中的值更改表单的某些特定行的颜色(需要更改每行“一个”字段的颜色)。例如,当我们打开表单时,我们需要在某一行的值为 1 的字段中看到绿色。如果该字段的值为 2,我们需要看到橙色,如果该字段的值为 3,则需要看到橙色。是红色的。
ID Criteria
201 1 --> the cell containing 1 should be colored in green
203 3 --> the cell containing 3 should be colored in red
204 3
205 --> the cell that contains nothing should be kept uncolored
206 1
207 2
Run Code Online (Sandbox Code Playgroud)
注意:当表单打开时,值(1、2 和 3)已经可用,并且它们也存储在表中。
为了解决这个问题,我使用了条件格式(我使用的是 Microsoft Office Access 2007)。
下面是相应的代码。
Option Compare Database
Option Explicit
Const GreenG As Byte = 1
Const OrangeO As Byte = 2
Const RedR As Byte = 3
Private Sub StartCondFormatting()
Dim objFrc As FormatCondition
Const orange As Long = 42495
Const green As Long = 25600
Const red As Long = 255
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Conditional" Then
With ctl
'Remove format conditions
.FormatConditions.Delete
'Create three format objects and add them to the FormatConditions
Set objFrc = .FormatConditions.Add(acExpression, acEqual, "[fieldName] = " & GreenG)
Set objFrc = .FormatConditions.Add(acExpression, acEqual, "[fieldName] = " & OrangeO)
Set objFrc = .FormatConditions.Add(acExpression, acEqual, "[fieldName] = " & RedR)
'Specify the formating conditions
.FormatConditions(0).BackColor = green
.FormatConditions(0).Enabled = True
.FormatConditions(1).BackColor = orange
.FormatConditions(1).Enabled = True
.FormatConditions(2).BackColor = red
.FormatConditions(2).Enabled = True
End With
End If
Next ctl
Set objFrc = Nothing
End Sub
Private Sub Form_Load()
StartCondFormatting
End Sub
Run Code Online (Sandbox Code Playgroud)