Highlight field background in continuous form

前端 未结 2 977
南旧
南旧 2021-01-21 15:07

I have an Access continuous form. I would like to change the forecolor of a specific record\'s field.

I have the field to highlight from the FieldModified field. So for

相关标签:
2条回答
  • 2021-01-21 15:32

    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)

    In conditional format wizard, you can create a condtion withExpression Isand[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 .

    You can use VBA to format all controls with FormatConditions by code. If you want to modify an existing condition use.Modifyinstead 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
    

    Update:
    You can use theDetails_Paintevent of the form to format same control different per record. This enables conditional format for controls withoutFormatConditionsproperty 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
    
    0 讨论(0)
  • 2021-01-21 15:55

    You can't use a String variable like this, fldName is an identifier holding a String value.. not an identifier - in Me.ControlName, both Me and ControlName are identifiers.

    But not all hope is lost! You can use a String to pull a Control object from a form!

    All form controls should exist in the form's Controls collection, keyed by name:

    Me.Controls(fldName).ForeColor = vbRed
    
    0 讨论(0)
提交回复
热议问题