以连续形式突出显示字段背景 [英] Highlight field background in continuous form
问题描述
我有一个Access连续表格.我想更改特定记录字段的前景色.
I have an Access continuous form. I would like to change the forecolor of a specific record's field.
我从FieldModified字段中突出显示该字段.因此,例如FieldModified ="Converted".转换为我表单上的字段.
I have the field to highlight from the FieldModified field. So for example FieldModified = "Converted". Converted being a field on my form.
我想更改已转换"字段的颜色,并对表单中的每个记录进行更改.
I would like to change the color of the "Converted" field, and do this for each record in the form.
我以为这段代码可以用,但是我遇到了一个错误.[FieldModified] .ForeColor.我需要对表单中的每个记录执行此操作.
I thought this code would work, but I get an error on Me.[FieldModified].ForeColor. And I need to do this for each record in the form.
代码:
Private Sub Form_Load()
Dim fldName As String
fldName = Me.FieldModified.value
If (Not IsNull(fldName)) Then
Me.[fldName].ForeColor = vbRed '<--doesn't recognize fldName value
End If
End Sub
更新了代码,但它给我一个错误438,表明对象不支持此属性或方法.但是该表单确实突出显示了表单中的字段,但是突出了一个字段"fldName"
Updated code but it gives me an error 438 saying object doesn't support this property or method. But the form does highlight fields on the form but it highlights more then the one field "fldName"
Private Sub Form_Load()
Dim rstForm As String
Dim fldName As String
Set rstForm = Me.ChangedData.Form.Recordset
Do While Not rstForm.EOF
fldName = Me.FieldModified.value
If (Not IsNull(fldName)) Then
Me.Controls(fldName).ForeColor = vbRed '<--doesn't recognize fldName value
End If
rstForm.MoveNext
Loop
End Sub
推荐答案
您设置控件的默认格式.连续形式的控件的每个副本都使用此格式.要根据条件(fldName = Me.FieldModified.value
)进行格式化,您需要按照安德烈(Andre)的说明进行条件格式化,或使用详细信息"部分的绘画事件(请参阅底部更新)
You set the default format for the control. Every copy of the control in the continuous form uses this format. To format by a condition (fldName = Me.FieldModified.value
) you need Condtional Formatting as Andre told you or use the detail-sections paint event (see update on bottom)
在条件格式向导中,如果名称与FiledModified
相匹配,则可以为要突出显示的表单的每个控件创建Expression Is
和[Converted].Name = [FieldModified]
的条件.在Ms Access表达式中,您不能使用Me
,只需将其忽略即可.
In conditional format wizard, you can create a condtion withExpression Is
and[Converted].Name = [FieldModified]
for each control of the form that should be highlighted, if its name matchesFiledModified
. In Ms Access expressions you can't useMe
, just omit it .
您可以使用VBA通过 FormatConditions 通过代码.如果要修改现有条件,请使用.Modify
而不是.Add
You can use VBA to format all controls with FormatConditions by code. If you want to modify an existing condition use.Modify
instead of.Add
Private Sub Form_Load()
Dim ctl As Access.Control
For Each ctl In Me.Controls ' loop through all controls of form
On Error Resume Next ' Not all controls can have conditional format (e.g. labels). To save the check of control type, we ignore errors here
ctl.FormatConditions.Add(acExpression, , ctl.Name & ".Name=[FieldModified]").BackColor = vbRed 'add o format condition to control if possible, else an error is raised but ignored
If Err.Number Then 'show errors
Debug.Print "Error: " & Err.Number & " - " & Err.description & " in Control: " & ctl.Name & " Type is " & TypeName(ctl)
Err.Clear 'reset error to catch next
Else
Debug.Print "FormatCondition added to Control: " & ctl.Name & " Type is " & TypeName(ctl)
End If
Next ctl
On Error GoTo 0 ' turn on errors again, maybe add an error handler (On Error Goto MyErrHandler)
End Sub
更新:
您可以使用窗体的Details_Paint
事件来格式化每个记录不同的相同控件.这样可以为没有FormatConditions
属性的控件(如标签,按钮)启用条件格式.
Update:
You can use theDetails_Paint
event of the form to format same control different per record. This enables conditional format for controls withoutFormatConditions
property like labels, buttons.
Private Sub Detail_Paint()
Dim c As Access.Control
For Each c In Me.Detail.Controls
If c.Name = Me.FieldModified.Value Then
c.ForeColor = vbRed
Else
c.ForeColor = vbBlack
End If
Next
End Sub
这篇关于以连续形式突出显示字段背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!