Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close form without code running? 2

Status
Not open for further replies.

thefourthwall

IS-IT--Management
Feb 26, 2002
387
US
Hi,

I have code behind the 'On Unload' event of a form that checks for data in 3 fields. If the data is missing, the user is prompted to enter the data in an input box for each field in succession.

But I have discovered there may be times when a user legitimately wants to back out of this form without entering any data; is there a way to close the form without having the 'on Unload' code run? I have searched the forum but not found this condition so far; if I missed it, I apologize.

Thank you,
-thefourthwall
 
Maybe:

Code:
Private Sub Form_Unload(Cancel As Integer)
    If IsNull(txtText1) Or IsNull(txtText2) _
        Or IsNull(txtText3) Then
    
        If MsgBox("You have not completed required fields. " & vbCrLf _
            & "Are you sure you wish to exit?", vbYesNo & vbwarning) <> vbYes Then
            'Whatever
            Cancel = True
        End If
    End If
End Sub

 

I have code behind the 'On Unload' event of a form that checks for data in 3 fields.
Your problem is that validation code such as this has no place in the Form_Unload event, it belongs in the Form_BeforeUpdate event.

Form_Unloadis too late to be checking/altering data, and as you've found out, having such code here prevents the normal closing of the form.

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
Remou:

Thanks -- took your code as a starting point, and now that I am back at work I will continue to see if that does it.

Your thinking helped to clarify mine.

The Missinglinq:

I did not realize that form_unload is too late (I'm still new to, and learning, Access) and yes, to my discomfort, see now that the form_unload event might be the wrong place. Trying out the code now on the Form_BeforeUpdate event. Thank you!
 

We've all been there! Good luck with your project!

The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
The Missinglinq:

Thanks! Hard to not feel inept sometimes, but the professional & courteous attitudes (and solutions) here helped me, to help someone else with information you provided on closing a form.

Grateful & appreciative -

thefourthwall
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top