Link to home
Start Free TrialLog in
Avatar of dloebig
dloebigFlag for United States of America

asked on

Cancel unload on main form if subform generates error

I have an event on my main form when it unloads to set the source object for it's subform to blank.  On the subform form I have unload events that check for values in fields and if it finds them it will display an option to fix it or ignore it.  If they want to fix it it cancels the unload and takes them to that field.  If they choose to ignore it then it continues with the unload.  What I'm trying to do is let the subform finish all of it's stuff first before he main form unloads.
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

I can certainly see the problem but there is a way around it.

The problem:  The Parent Form will Unload before the Unload Event of the Child Fires, hence, canceling the unload event of the child form does nothing after then parent has already closed.

Solution...  

1) Write Some code in the Sub Form that looks Something like this.  Make sure you don't put the word Private in front of the Function Declaration.

Function OK2Unload(Cancel As Integer)
    'Write your Code here.  Do any validation, action you like but if you want to cancel the Unload event of the parent set the value of Cancel to True (Cancel = True)
    If (<< expression to determin whether conditions are not right for closing/unloading  >>) Then
        Cancel = True
    End If
End Function


2) In the Parent Form's Unload event add a line of code to call the child form's Function where it will find out whether it is ok to unload or not.

Private Sub Form_Unload(Cancel As Integer)
    Me.frmMySubForm.Form.OK2Unload Cancel
End Sub

'Rick

Avatar of dloebig

ASKER

It works canceling the unload but with some quirks.  

1.  Is there a way to bypass the error handling for the On Close Event?  If I select yes to fix it I get a "Close action was canceled error"

How to bypass any error handling within a procedure...

Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
     'Write your Code here (any errors will be ignored and Access will merely move on)
End Sub

Rick
Avatar of dloebig

ASKER

Woking Great One more thing. Is there a way to bring me to the field that contained the error during this process?  This is what I had before:

If Response = vbYes Then
    Cancel = True
    DoCmd.GoToControl "IMR-Operator"

This is done during the OK2Unload function in the sub-  When I try to run it now I get an error that say's "IMR-Operator" does not exist.  

ASKER CERTIFIED SOLUTION
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dloebig

ASKER

Had to change a few things around but got it working.  I was trying to set focus on a field on the subfrom without seeting focus on the subform first.  Thanks!
Setting focus on a control within a sub form reliably does (unfortunately) require setting focus to the sub form's control first.  In any case I'm glad to see you got it working and pleased to have been of help.

Rick