【问题标题】:Access VBA 2016 - Set Border Colour for each control if emptyAccess VBA 2016 - 如果为空,则为每个控件设置边框颜色
【发布时间】:2022-01-11 21:59:27
【问题描述】:

我正在尝试遍历所有文本框/组合框,如果它们为空,则将其边框颜色更改为红色。我已经尝试过使用“处理焦点”功能的先前解决方案,但是当我单击文本框/组合框时,边框颜色才变为红色。

  1. 如何在不点击的情况下更改边框颜色?
  2. 其次,如何让标签名称显示在msgbox中而不是控件的名称?

上一个例子:https://www.access-programmers.co.uk/forums/threads/change-border-color-of-textboxes-when-they-have-focus.246363/

Public Function validateCase() As Boolean
Dim ctrl As Control

For Each ctrl In Me.Controls
    If IsNull(ctrl) Then
        If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
            MsgBox (ctrl.Name & " is Empty")
            ctrl.OnGotFocus = "=HandleFocus([" & ctrl.Name & "], True)"
        End If
    End If
Next ctrl    
End Function

Public Function HandleFocus(ByRef ctrl As Control, ByVal blnFocus As Boolean)
If blnFocus = True Then
    ctrl.BorderColor = RGB(255, 0, 0)
Else
    ctrl.BorderColor = RGB(0, 0, 0)
End If
End Function

【问题讨论】:

    标签: vba ms-access-2016


    【解决方案1】:
    ctrl.LabelName.caption
    

    将 LabelName 更改为您的标签名称

    【讨论】:

      【解决方案2】:

      对您自己回答的有关边框的问题的简短说明:绝对没有理由在设置边框颜色之前设置焦点。您误解了您提到的问题的目的:控件获得焦点时更改边框颜色的想法。
      请注意,您应该只问一个问题Stackoverflow 上的时间。如果您需要了解两件事,请分别提出两个问题。


      要查找文本框(或类似控件)的标签,请使用控件的控件属性。标签存储为controls(0)。以下 2 个函数分别获取控件的标签控件。这是标题:

      Function getLabelCaption(ctrl As Control) As String
          Dim ctrlLabel As label
          Set ctrlLabel = getLabel(ctrl)
          If Not ctrlLabel Is Nothing Then getLabelCaption = ctrlLabel.Caption
      End Function
      
      Function getLabel(ctrl As Control) As label
          On Error Resume Next
          If TypeName(ctrl.Controls(0)) = "Label" Then
              Set getLabel = ctrl.Controls(0)
          End If
          On Error GoTo 0
      End Function
      

      在您的代码中,您可以使用类似

      Dim caption as String
      caption = getLabelCaption(ctrl)
      MsgBox Iif(caption = "", ctrl.name, caption) & " is Empty")
      

      【讨论】:

        【解决方案3】:

        我有一个部分答案。

        我通过在ctrl.OnGotFocus 之后添加一行ctrl.SetFocus 解决了第一个问题。

        我没有第二个问题的答案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-23
          • 1970-01-01
          • 1970-01-01
          • 2013-09-16
          • 2010-12-20
          • 2013-06-03
          • 2015-11-29
          相关资源
          最近更新 更多