How to set conditional formatting for forms, reports, and datasheet using VBA

耗尽温柔 提交于 2019-12-11 17:53:21

问题


Well, this is the follow up on another thread.

I have a Persontable with a several fields

The table is presented to the user in 3 different ways:

1) as a datasheet

2) as a table

3) as a report (no picture, but is similar to datasheet)

A special tag §Person§ is added to the Form, to select main the attached table

Also, there is a query returning the errors and their formatting rules for each field in a given table

here is the piece of code i would use to define the conditionalformattingrules for each Form, Report, Datasheet, when they are loaded.

    Private Sub Form_Open(Cancel As Integer)
      Call validazione.validate(Form)
    End Sub

    Function validate(aForm As Form)
      Call DeleteFormats(aForm)       ' to remove existing formats
      Call setFormats(aForm)          ' to insert the new formats
    End Function

The setFormat sub is the following:

     Sub setFormats(aForm As Form)
      Dim TableName As String, t() As String, ErrSQL As String, ctlName As String
      Dim ErrRst As DAO.Recordset, FormCtl As Control, ctl As Variant
      Dim frmtCount As Integer, Cnt As Integer
      Dim fcdSource As FormatCondition, fcdDestination As FormatCondition
      Dim varOperator As Variant, varType As Variant, x As Variant, y As Variant
      Dim varExpression1 As Variant, varExpression2 As Variant
      Dim intConditionCount As Integer, intCount As Integer

      If Len(aForm.Tag) > 0 And Mid$(aForm.Tag, 1, 1) = "§" Then
        t = Split(aForm.Tag, "§", 1)
        If Len(t(0)) > 0 Then TableName = t(0)
        TableName = Replace(TableName, "§", "")

        ErrSQL = "SELECT * FROM [Q Errori per tabella] WHERE ([TableName] = """ & TableName & """);"
        Set ErrRst = CurrentDb.OpenRecordset(ErrSQL, , dbReadOnly)

        If ErrRst.EOF Then Exit Sub

    ' ***IT RUNS UNTIL HERE***

          FormCtl = aForm.Controls("CAP")
          y = aForm.Properties(ctlName).Item("Codice Fiscale")


        With ErrRst
            .MoveFirst
            Do Until .EOF
              x = ErrRst.Fields("FieldName")
              Debug.Print x

             ' On Error GoTo fine
              FormCtl = aForm.Controls("CAP")
              FormCtl = aForm.

              On Error GoTo 0
              If FormCtl.ControlType = acTextBox Or FormCtl.ControlType = acComboBox Then
              ' Add the FormatCondition
              Cnt = FormCtl.FormatConditions + 1
              FormCtl.FormatConditions.Add acExpression, , .Fields.Item(Cnt).Value

            ' Reference the FormatCondition to apply formatting.
            ' Note: The FormatCondition cannot be referenced
            ' in this manner until it exists.
              Set fcdDestination = ctl.FormatConditions.Item(Cnt).Value

              With FormCtl.FormatConditions.Item(Cnt)
                  .BackColor = Eval("RGB" & ErrRst.item("Sfondo").value)
        '         .FontBold = fcdSource.FontBold
        '         .FontItalic = fcdSource.FontItalic
        '         .FontUnderline = fcdSource.FontUnderline
                  .ForeColor = Eval("RGB" & ErrRst.item("pen").value)
              End With
            End If
    fine: On Error GoTo 0
        '  Next x
         .MoveNext
       Loop
      End With
     End If
    End Sub

***Well, it looks crazy, but my problem is how to access a formfield, or a reportcolumn, or a datasheet column by name.

Also, I would like to have just one function to manage all the three options: Form, Report and Datasheet.


Can anyone help? TIA Paolo

来源:https://stackoverflow.com/questions/55733649/how-to-set-conditional-formatting-for-forms-reports-and-datasheet-using-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!