Stopping a function

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

Hi,
I have a function set in the lost focus of a date control
to ensure that a date is entered.
When I use the exit button on the form I would like for
that function not to trigger, could someone help ?
Thanks in advance
 
Move the code into the BeforeUpdate event of the form.

The Exit event of the control does not fire if the user never enters the
control in the first place (e.g. if the user clicks somewhere else).
Form_BeforeUpdate is the only event that can safely check for Nulls. As a
bonus, it does not fire if the record is undone, so if solves the problem
you asked about.

Actually, you can avoid the problem altogether if you set the Required
property of the field in the table.
 
Thanks for your suggestion Allen, but unfortunately it
wasn’t what I was looking for.
What I have is a date field that must have a date entered
to allow the list box to show all previous entries for
that date.
So I set up the following function

Private Sub Date2_LostFocus()
If IsNull(Date2) Then
MsgBox "You must enter a DATE!", , "CHLOE"
txtJobNumber.SetFocus
Date2.SetFocus
Else
txtsetfocus.SetFocus
End If
End Sub

But what I need now is if they open the form and then want
to just exit, then using the exit button stops the "date2_
lostfocus function" from triggering and the form is just
exited

Private Sub butExit_Click()
DoCmd.Close
End Sub

Thanks
 
If this is a bound form, Form_BeforeUpdate is the solution.

If this is unbound, and you want to permit the value to be Null then
reassign the RowSource of the ListBox in the AfterUpdate event of your text
box:

Private Sub Date2_AfterUpdate()
Dim strSQL As String

If IsNull(Me.Date2) Then
strSQL = "SELECT * FROM MyTable WHERE (False);"
Else
strSQL = "SELECT * FROM MyTable WHERE ([MyDate] = " &
Format(Me.Date2, "\#mm\/dd\/yyyy\#") & ");"
EndIf
Me.[MyListBox].RowSource = strSQL
End Sub
 
Back
Top