[英]Access Conditional Formatting - Fewer VBA Options over user interface?
問題是,雖然前端允許 4 個或更多條件,但當我嘗試使用 VBA 設置條件時,我在設置第 4 個條件時遇到了錯誤。 換句話說,如果我只嘗試在代碼中設置 3 個條件,那么代碼運行良好。
我正在使用 MS Access 2010。我需要為連續表單上的兩個文本框設置條件格式。 我知道舊版本的 MS Access 只允許文本框上的 3 個條件,但我知道我可以在 Access 2010 中獲得更多條件。我當前的應用程序有 4 個使用用戶界面的條件。 在我對這個問題的研究中,有人說更高版本的 MS Access 允許多達 50 個條件。 無論哪種方式,我都無法確認這一點,即使我查看了 Access 2010 規范頁面。 但我知道我至少可以得到 3 個以上的條件。
以下是適用於最多 3 條記錄的測試代碼:
Function fApplyConditionFormatNow()
Dim objFormatConds As FormatCondition
Dim i As Integer 'index number for specific format conditions
Dim stSQL As String 'query to get list of categories
Dim rs As DAO.Recordset
i = 0
'clear out just in case FormatConditions accidentially got saved
'with the form at some point.
Me.ID.FormatConditions.Delete
'get a recordset containing the formatting information
'(ie, get RGB values for each category type)
stSQL = "SELECT * FROM tblTestConditionalFormatting;"
fRunSQL stSQL, rs 'fRunSQL is custom code that gets runs stSQL and returns the recordset
'loop through recordset to get conditional formatting values
Do Until rs.EOF
'create a condition on textbox named "ID". The condition will be for
'the Category/Type (TypeNm) that's up now in the recordset.
Set objFormatConds = Me.ID.FormatConditions.Add(acExpression, , "[TypeNm] = '" & rs!TypeNm & "'")
'add formatting for the condition just created.
With Me.ID.FormatConditions(i)
.BackColor = RGB(rs!RGB1, rs!RGB2, rs!RGB3)
End With
i = i + 1
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Function
當類別表中包含 4 條記錄時:即 tblTestConditionalFormatting,我收到以下錯誤:“運行時錯誤 7966:您指定的格式條件大於格式條件的數量。”
那么,前端可以處理超過3個條件,而VBA對象只能處理3個條件,這似乎是一個錯誤? 或者也許我做錯了什么。 有沒有其他人遇到過這個? 你有解決辦法的想法嗎?
謝謝!!
而不是With Me.ID.FormatConditions(i)
,使用您剛剛創建的FormatCondition
對象:
Set objFormatConds = Me.ID.FormatConditions.Add(acExpression, , "[TypeNm] = '" & rs!TypeNm & "'")
'add formatting for the condition just created.
With objFormatConds
.BackColor = RGB(rs!RGB1, rs!RGB2, rs!RGB3)
End With
你不再需要i
了。
我有類似的代碼(簡化)如下:
For i = 1 To 6
lColor = DLookup("Color", "someTable", "p_Color_ID = " & i)
Set objFrc = txtFld.FormatConditions.Add(acExpression, , "[ColorGroup] = '" & i & "'")
objFrc.BackColor = lColor
Next i
編輯
顯然,如果您不觸摸 0..2,您可以編輯 FormatConditions >= 3:
https://access-programmers.co.uk/forums/showthread.php?t=271679
也許我有這樣的事情......:/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.