A
Andrew
I have the usual validation preventing a user from a leaving a field when it
is required. The code is found several places on this great site. It is:
-------------------------------------------------------------------
Private Sub txtName_Exit(Cancel As Integer)
Dim strmsg As String
If Nz(txtName, "") = "" Then
strmsg = "This field is required!"
MsgBox strmsg, vbExclamation, "Uh-Oh!"
Cancel = True
End If
End Sub
-------------------------------------------------------------------
It works. However, if the user starts a record and decides to quit by
hitting escape and then closing the form, the message show twice presumably
because the event fires twice.
When I move the code to a function, the code doesn't fire twice when the
form closes. However, it no longer prevents a user from leaving the Name
field if he tries to move to the next field without entering a value. Here
is my code with the validation moved to a function:
---------------------------------------------------------------------------------
Private Sub txtName_Exit(Cancel As Integer)
Dim bResult As Boolean
Cancel = Messages(bResult)
End Sub
-------------
Public Function Messages(Cancel As Boolean)
Dim strmsg As String
' This tests for null and a blank string
If Nz(txtName, "") = "" Then
strmsg = "This field is required!"
MsgBox strmsg, vbExclamation, "Uh-Oh!"
Cancel = True
End If
End Function
is required. The code is found several places on this great site. It is:
-------------------------------------------------------------------
Private Sub txtName_Exit(Cancel As Integer)
Dim strmsg As String
If Nz(txtName, "") = "" Then
strmsg = "This field is required!"
MsgBox strmsg, vbExclamation, "Uh-Oh!"
Cancel = True
End If
End Sub
-------------------------------------------------------------------
It works. However, if the user starts a record and decides to quit by
hitting escape and then closing the form, the message show twice presumably
because the event fires twice.
When I move the code to a function, the code doesn't fire twice when the
form closes. However, it no longer prevents a user from leaving the Name
field if he tries to move to the next field without entering a value. Here
is my code with the validation moved to a function:
---------------------------------------------------------------------------------
Private Sub txtName_Exit(Cancel As Integer)
Dim bResult As Boolean
Cancel = Messages(bResult)
End Sub
-------------
Public Function Messages(Cancel As Boolean)
Dim strmsg As String
' This tests for null and a blank string
If Nz(txtName, "") = "" Then
strmsg = "This field is required!"
MsgBox strmsg, vbExclamation, "Uh-Oh!"
Cancel = True
End If
End Function