Prevent a Form from closing.

ksmithson

Registered User.
Local time
Today, 16:38
Joined
Jul 14, 2010
Messages
11
Hey guys,

I am looking for a way to prevent a form from closing until nulls in a combo box or text box are filled. I am currently using the Before Update event but if you close the form, Access will say that the reports in the form will not be saved. Is there anyway around this?
 
In the OnClose event, check your fields for nulls. If you find any, pop up a msgbox for the user and use DoCmd.CancelEvent to stop the form from closing.
 
Actually you can use the Cancel argument in the Unload event instead.
 
The DoCmd.CancelEvent line did not work.
Here is the code:

Code:
Private Sub Form_Close()
If Me.cboxTeamWorkScore.ListIndex = -1 Or Me.cboxReliabilityScore.ListIndex = -1 Or Me.cboxAdaptabilityScore.ListIndex = -1 Then
MsgBox "Please select a score."
DoCmd.CancelEvent
Me.cboxTeamWorkScore.SetFocus
End If
End Sub
 
As mentioned previously, you can use the Unload event, there's a Cancel paramter there.
Code:
Private Sub Form_Unload(cancel as integer)
    If Me.cboxTeamWorkScore.ListIndex = -1 Or Me.cboxReliabilityScore.ListIndex = -1 Or Me.cboxAdaptabilityScore.ListIndex = -1 Then
        Cancel = True
        MsgBox "Please select a score."
        Me.cboxTeamWorkScore.SetFocus
    End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom