- Local time
- Today, 06:42
- Joined
- Feb 19, 2002
- Messages
- 47,293
Your validation code Must go in the Form's BeforeUpdate event if it involves checking for empty values or comparisons between multiple values.
To keep the form from closing after displaying the error, you have to cancel the form's unload event. To know whether to cancel the event requires a form level variable that is set in various events.
To keep the form from closing after displaying the error, you have to cancel the form's unload event. To know whether to cancel the event requires a form level variable that is set in various events.
Code:
Option Compare Database
Option Explicit
Private bError As Boolean
....
Private Sub Form_Current()
bError = False
....
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.SomeField & "" = "" Then ' control is empty
Cancel = True
bError = True
Me.SomeField.SetFocus
Exit Sub
End If
....
Private Sub Form_Unload(Cancel As Integer)
If bError = True Then
Cancel = True
bErrror = False
Exit Sub
End If