[英]MS Access VBA to see if a conditional formatting rule is currently active for a control
我有一個條件格式規則,可以根據之前的選擇啟用某些字段。 默認情況下禁用這些字段。 一旦啟用這些字段,它們也需要輸入。 但是,由於它們是有條件的,所以我不能在數據庫級別上要求它們。
我嘗試過的是檢查提交處理程序是否啟用了控件並且不為空。
Public Sub SaveButton_Click()
For Each ctl In Me.Controls
If (ctl.Tag = "ConditinallyRequiredField") Then
If (ctl.Enabled = True) Then
Debug.Print "This is never reached"
' Check for empty values.
If (Len(ctl.Value & vbNullString) = 0) Then
MsgBox "One or more required fields are missing input values."
GoTo stopSubmit
End If
End If
End If
Next ctl
... Do submit
End Sub
但是ctl.Enabled
始終是 false,無論我在什么時間點檢查它。 所以看起來條件格式會覆蓋它而不影響實際屬性。
因此,由於ctl.Enabled
顯然始終為假,我想檢查給定控件是否有條件格式規則處於活動狀態(只有一個)。 但到目前為止我還沒有發現這樣做。 可以嗎?
經過幾個小時的擺弄,我想出了一個方法,它使用 SetFocus 方法來確定是否啟用了一個字段。 禁用的字段無法獲得焦點並會引發錯誤。
這個概念已經包含在 function 中,用於測試並返回 True 或 False。
' Helper function. Returns True if the textfield could receive focus.
Private Function TestEnabled(ctl As Control) As Boolean
On Error GoTo noFocus
' Assume the textfield can be focussed.
TestEnabled = True
' Try setting the focus.
ctl.SetFocus
Exit Function
noFocus:
' If the textfield could not receive focus it is not enabled.
TestEnabled = False
End Function
在提交處理程序中,它被包裝在一個 if 語句中以進行測試。 如果該字段已啟用但為空,則代碼會中斷,但該字段仍保持焦點。 這是一個額外的好處,可以直接指導丟失的字段。 測試完所有字段后,焦點將重置為提交按鈕(就像用戶單擊它一樣),以防止檢查的最后一個字段保持焦點。
' Submit handler
Public Sub SaveButton_Click()
For Each ctl In Me.Controls
' Only test the tagged fields to prevent labels, etc. from getting tested.
If (ctl.Tag = "ConditinallyRequiredField") Then
If (TestEnabled(ctl)) Then
' Check for empty values.
If (Len(ctl.Value & vbNullString) = 0) Then
MsgBox "One or more required fields are missing input values."
GoTo stopSubmit
End If
End If
End If
Next ctl
' Set Focus to the save button again.
Me.SaveButton.SetFocus
' ... Do submit
End Sub
請注意,June7 建議在 VBA 中再次從條件格式中簡單地測試相同的表達式也有效。 但由於它基於 DLookup,在通過 VPN 工作時已經導致明顯的減速,我想避免再次運行該表達式。
另請注意,這並沒有提供問題的答案,但確實解決了我的問題:)
由於 CF 的動態特性,我懷疑是否可以確定是否在任何特定時間應用條件格式。 沒有任何 CF 屬性/方法提供此指標。 很可能是為什么找不到示例代碼。 CF 規則中用於啟用/禁用的相同條件可用於 VBA 以確定控件的 state。
可以確定控件是否有任何 CF 規則。
ctl.FormatConditions.Count
只有文本框和組合框可以有 CF,所以請確保循環代碼只測試這些控件。 其他任何事情都會觸發“不支持此屬性”錯誤。
他們說永遠不會太晚,所以這是我關於這個主題的解決方案:注意它只適用於“表達式”類型的 CF,並且所有條件都必須使用控件的完整路徑 - [Forms],[FormName].[ControlName] = some_condition,而不是短路徑 - [ControlName] = some_condition,話雖如此,它就像遍歷所有條件一樣簡單。 測試它們是否 EVAL 為 TRUE,當第一個為 TRUE 時 - 我們有一個贏家。
Public Function GetActiveFC(Ctrl As Access.control) As FormatCondition
Const C_PROC_NAME = "mdl_Functii2022\GetActiveFC"
On Error GoTo Eroare
Dim I As Integer
If TypeOf Ctrl Is Access.TextBox Or TypeOf Ctrl Is Access.ComboBox Then
If Ctrl.FormatConditions.Count = 0 Then
Set GetActiveFC = Nothing 'there are no FCs
Else
For I = 0 To Ctrl.FormatConditions.Count - 1
If Eval(Ctrl.FormatConditions(I).Expression1) Then
Set GetActiveFC = Ctrl.FormatConditions(I)
Exit For
End If
Next
End If
Else
Set GetActiveFC = Nothing 'the control does not allow FC
End If
Iesire:
Exit Function
Eroare:
Debug.Print C_PROC_NAME, Err.Number, Err.Description
Set GetActiveFC = Nothing
Resume Iesire
End Function
並使用它:
Dim FC as FormatCondition
Set FC = GetActiveFC([ControlName])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.