2011-01-28 59 views
0

此代码无法正常工作。谁能帮我吗?onLost焦点事件不起作用-ms访问

Private Sub txtErrorComment_5_LostFocus() 

    If IsEmpty(Me.txtErrorComment_5) Then 
     MsgBox "Eh..! It cannot be Empty." 
    End If 

End Sub 
+1

“Empty”是VB(A)中Variant类型未初始化变量的特殊值。这并不意味着你会这么想。 – RolandTumble 2011-01-28 18:12:41

+3

LostFocus对此是错误的事件。相反,您应该使用BeforeUpdate。 – 2011-01-28 23:42:45

+0

@ David-W-Fenton:听到,听到。 – phoog 2011-01-29 19:34:07

回答

1

txtErrorComment_5可能是空 - 时通过的IsEmpty运行的计算结果为假() - 是的,这是联合国直观,但每当你比较空,结果是假的。

尝试以下操作:

Private Sub txtErrorComment_5_LostFocus() 

    If IsEmpty(Me.txtErrorComment_5) or IsNull(Me.txtErrorComment_5) Then 
     MsgBox "Eh..! It cannot be Empty." 
    End If 

End Sub 

或者只是:

Private Sub txtErrorComment_5_LostFocus() 

    If IsNull(Me.txtErrorComment_5) Then 
     MsgBox "Eh..! It cannot be Empty." 
    End If 

End Sub 

ISNULL可以处理空的

编辑:

@大卫-W-Fenton法是正确的。更好的方式来做到这一点更新前(未引发LostFocus)

Private Sub txtErrorComment_5_BeforeUpdate(Cancel As Integer) 

    If IsNull(Me.txtErrorComment_5) Then 
     MsgBox "Eh..! It cannot be Empty." 
     Cancel=true 
    End If 

End Sub 
1

为了检测txtErrorComment_5是否是空白的,连接一个空字符串到它的值,看看联合表达的字符长度是否为零。

Private Sub txtErrorComment_5_LostFocus() 
    If Len(Me.txtErrorComment_5 & vbNullString) = 0 Then 
     MsgBox "Eh..! It cannot be Empty." 
    End If 
End Sub 

如果你想,当它仅包含一个或多个空格字符txtErrorComment_5视为空白,包括TRIM()函数。

Private Sub txtErrorComment_5_LostFocus() 
    If Len(Trim(Me.txtErrorComment_5) & vbNullString) = 0 Then 
     MsgBox "Eh..! It cannot be Empty." 
    End If 
End Sub