2012-05-22 30 views
1

我会假设的东西会出现很多...

我想知道是否有办法在Access的条件格式中格式化所有空白字段。在我的情况下,通常需要输入所有字段,但并非在所有情况下都是。所以,与其写一堆条件代码来限制用户写在那里,我只是想在我的领域中出现一些红色背景作为提醒:“嘿,这里什么都没有......当然,那是你想要的吗?”

这是在平板电脑上,所以消息框会很烦人。所以条件格式化是。我知道你可以有'空'([场]),但这需要我通过30+场上的20+表格,并确保适当的字段名称等,然后单独为他们输入条件。有没有一种方法可以简单地多选我的领域,做一个条件格式在多个,并使用可能是“等于:NULL”?

我试过“等于:空”,它不工作..也不“等于:“”“(使用访问常量)想法为什么?或者我怎么解决这个问题?另外,它只对非触摸字段是必需的,所以如果用户开始键入然后删除回到空白,我不如果有更好的方法来做到这一点我所有的眼睛是空的条件格式 - MS Access窗体

编辑:我已经开始做一些VBA代码,我将粘贴到我的所有窗体中:

Private Sub Form_Load() 
Dim ctl As Control 
Dim reqCol As Long 
Dim focusCol As Long 
Dim doneCol As Long 
Dim format As FormatCondition 

reqCol = RGB(246, 180, 180) 
focusCol = RGB(252, 249, 238) 
doneCol = RGB(255, 255, 255) 

For Each ctl In Me.Controls 
    With ctl 
     Me.Controls(ctl.Name).FormatConditions.Delete 'Delete the existing conditions. 
     Me.Controls(ctl.Name).BackColor = doneCol 'Set the background color to the done color. 

     Select Case .ControlType 
      Case acTextBox 
       'Create the format objects. 
       format = Me.Controls(ctl.Name).FormatConditions.Add(acFieldValue, acEqual, "") 
       format = Me.Controls(ctl.Name).FormatConditions.Add(acFieldHasFocus) 

       'Format the filled in boxes (ie set back to red) 
       With Me.Controls(ctl.Name).FormatConditions(0) 
        .BackColor = reqCol 
        .Enabled = True 
       End With 

       'Format the current field color (ie set to beige) 
       With Me.Controls(ctl.Name).FormatConditions(1) 
        .BackColor = focusCol 
        .Enabled = True 
       End With 
     End Select 
    End With 
Next ctl 
End Sub 

问题是FormatConditions.Add(acFieldValue, acEqual, "")出于同样的原因不起作用......我该如何解决这个问题?看到VBA和内置条件都有缺陷,看起来像一个错误。或者我在我面前丢失了一些东西。

+0

将null与空字符串连接总是会产生一个空字符串。 'len([Fieldname]&“”)= 0'是否适合你的任务? – SeanC

+0

不是真的..它仍然留下了一个事实,即我必须为每个字段手动输入[Fieldname]。我需要批量解决方案。我已经开始在VBA中编写它,但它也有同样的问题。将更新问题。 – StuckAtWork

回答

1

将默认格式设置为想要显示零长度数据的方式。 使用

Field Value Isgreater than''

的条件格式,并设置格式应该如何与该领域的文本显示。

Screenshot of conditional formatting

您可以选择多个字段使用Shift +单击在设计视图中选择所有相应的字段,这需要应用到

+0

这是我最初做的事情,但它引起了一些小问题;在打字/导航时它会闪烁为红色。无论是否已选择任何内容,它也使下拉菜单变为红色;很难阅读。我会尝试少于“或0虽然! – StuckAtWork

0

解决。把这个在我的形式(可能会考虑使其成为一个模块;新本,不知道如何还)诀窍

Private Sub Form_Load() 
On Error Resume Next 

Dim ctl As Control 
Dim reqCol As Long 
Dim focusCol As Long 
Dim doneCol As Long 
Dim format As FormatCondition 
Dim expr As String 

reqCol = RGB(246, 180, 180) 
focusCol = RGB(252, 249, 238) 
doneCol = RGB(255, 255, 255) 

For Each ctl In Me.Controls 
    With ctl 
     'Delete the existing formatting 
     Me.Controls(ctl.Name).FormatConditions.Delete 
     Me.Controls(ctl.Name).BackColor = doneCol 

     Select Case .ControlType 
      Case acTextBox 
       expr = "IsNull(" & ctl.Name & ") = True" 
       'Create the format objects. 
       format = Me.Controls(ctl.Name).FormatConditions.Add(acFieldHasFocus) 
       format = Me.Controls(ctl.Name).FormatConditions.Add(acExpression, , expr) 

       'Format the filled in boxes (ie set back to focus color) 
       With Me.Controls(ctl.Name).FormatConditions(0) 
        .BackColor = focusCol 
        .Enabled = True 
       End With 

       'Format the current field color (ie set to required color) 
       With Me.Controls(ctl.Name).FormatConditions(1) 
        .BackColor = reqCol 
        .Enabled = True 
       End With 
     End Select 
    End With 
Next ctl 
End Sub 

是如何把它进入FormatConditions.Add(...)。正是我现在想要的。