onLost focus Event is not working -ms access
This code is not working. Can anyone help me out?
Private Sub txtErrorComment_5_LostFocus()
If IsEmpty(Me.txtErrorComment_5) Then
MsgBox "E开发者_如何学编程h..! It cannot be Empty."
End If
End Sub
txtErrorComment_5 is probably Null - which evaluates to False when run through IsEmpty() - yes, this is un-intuitive, but whenever you compare to Null, the result is false.
Try the following:
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
Or just:
Private Sub txtErrorComment_5_LostFocus()
If IsNull(Me.txtErrorComment_5) Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub
IsNull can handle Null's
Edit:
@David-W-Fenton is correct. The better way to do this BeforeUpdate (not 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
To detect whether txtErrorComment_5 is blank, concatenate a Null string to its value and see whether the character length of the combined expression is zero.
Private Sub txtErrorComment_5_LostFocus()
If Len(Me.txtErrorComment_5 & vbNullString) = 0 Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub
If you want txtErrorComment_5 treated as blank when it contains only one or more space characters, include the Trim() function.
Private Sub txtErrorComment_5_LostFocus()
If Len(Trim(Me.txtErrorComment_5) & vbNullString) = 0 Then
MsgBox "Eh..! It cannot be Empty."
End If
End Sub
精彩评论