检查字段是否在 Access 2013 中没有焦点
Check if a field does not have focus in Access 2013
我有两个组合框需要在表格的其余部分之前填写。如果用户移动到第三个框而前两个框之一仍然是空的,我想显示一条错误消息。
我尝试在 Form_Current
中使用以下内容,但如果我从 Combo1 移动到 Combo2,它会显示错误。它应该只显示我是否移动到不是两者之一的对象。
If Me.ActiveControl<>Combo1 And Me.ActiveControl<>Combo2 And (IsNull(Combo1.Value) Or IsNull(Combo2.Value) Then
' Error
Else
' No Error
End If
编辑:
这是我的工作代码。
Dim frm As Form
Dim ctl As Control
'loop through all controls in form
For Each ctl In frm.Controls
'only combo boxes
If ctl.ControlType = acComboBox Then
'ensure both boxes are filled in
If Not IsNull(Combo1.Value) And _
Not IsNull(Combo2.Value) Then
'enable controls on form
ctl.Enabled = True
Else
'disable controls on form
ctl.Enabled = False
End If
End If
Next
您可以为将所有其他字段的 Enabled
属性 设置为 No
的表单执行 On Current
宏,除非前两个字段具有有效条目。
试试这个:
If Me.ActiveControl.Name <> Me!Combo1.Name And _
Me.ActiveControl.Name <> Me!Combo2.Name Then
If IsNull(Me!Combo1.Value + Me!Combo2.Value) Then
' Error
Else
' No Error
End If
End If
Form_Current 以及大多数事件只有在当前记录集中的记录以某种方式被关注时才会触发。因此,如果您的组合框未绑定到记录,则无法检测表单中记录事件的更改。因此我建议使用绑定到组合框本身的事件。对两个组合框都使用 Combo_LostFocus(),或者如果您只有 1 个其他元素,则对那个元素使用 Combo_GotFocus() 或 _MouseDown。
示例解决方案可能如下所示:
选项比较数据库
Private Function IsValid() As Boolean
IsValid = False
If Not IsNull(Me.Combo0.Value + Me.Combo1.Value) Then IsValid = True
End Function
Private Sub Combo0_LostFocus()
Me.Combo2.Locked = Not IsValid()
End Sub
Private Sub Combo1_LostFocus()
Me.Combo2.Locked = Not IsValid()
End Sub
Private Sub Combo2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not IsValid() Then
MsgBox "you need to fill in the others first!"
Me.Combo2.Value = ""
End If
End Sub
我有两个组合框需要在表格的其余部分之前填写。如果用户移动到第三个框而前两个框之一仍然是空的,我想显示一条错误消息。
我尝试在 Form_Current
中使用以下内容,但如果我从 Combo1 移动到 Combo2,它会显示错误。它应该只显示我是否移动到不是两者之一的对象。
If Me.ActiveControl<>Combo1 And Me.ActiveControl<>Combo2 And (IsNull(Combo1.Value) Or IsNull(Combo2.Value) Then
' Error
Else
' No Error
End If
编辑: 这是我的工作代码。
Dim frm As Form
Dim ctl As Control
'loop through all controls in form
For Each ctl In frm.Controls
'only combo boxes
If ctl.ControlType = acComboBox Then
'ensure both boxes are filled in
If Not IsNull(Combo1.Value) And _
Not IsNull(Combo2.Value) Then
'enable controls on form
ctl.Enabled = True
Else
'disable controls on form
ctl.Enabled = False
End If
End If
Next
您可以为将所有其他字段的 Enabled
属性 设置为 No
的表单执行 On Current
宏,除非前两个字段具有有效条目。
试试这个:
If Me.ActiveControl.Name <> Me!Combo1.Name And _
Me.ActiveControl.Name <> Me!Combo2.Name Then
If IsNull(Me!Combo1.Value + Me!Combo2.Value) Then
' Error
Else
' No Error
End If
End If
Form_Current 以及大多数事件只有在当前记录集中的记录以某种方式被关注时才会触发。因此,如果您的组合框未绑定到记录,则无法检测表单中记录事件的更改。因此我建议使用绑定到组合框本身的事件。对两个组合框都使用 Combo_LostFocus(),或者如果您只有 1 个其他元素,则对那个元素使用 Combo_GotFocus() 或 _MouseDown。
示例解决方案可能如下所示: 选项比较数据库
Private Function IsValid() As Boolean
IsValid = False
If Not IsNull(Me.Combo0.Value + Me.Combo1.Value) Then IsValid = True
End Function
Private Sub Combo0_LostFocus()
Me.Combo2.Locked = Not IsValid()
End Sub
Private Sub Combo1_LostFocus()
Me.Combo2.Locked = Not IsValid()
End Sub
Private Sub Combo2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not IsValid() Then
MsgBox "you need to fill in the others first!"
Me.Combo2.Value = ""
End If
End Sub