检查字段在Access 2013中是否没有焦点

时间:2016-07-26 18:12:03

标签: vba ms-access access-vba ms-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

3 个答案:

答案 0 :(得分:1)

您可以为表单设置On Current宏,将所有其他字段“Enabled属性设置为No,除非前两个字段包含有效条目。

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分:1)

Form_Current以及大多数事件只会在以某种方式涉及当前记录集的记录时触发。因此,如果组合框未绑定到记录,则无法检测表单中记录事件的更改。因此,我建议使用绑定到comboxes本身的事件。对两个组合框使用Combo_LostFocus(),或者如果你只有另外一个元素,则使用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
相关问题