Solved

Cancel unload on main form if subform generates error

Posted on 2006-06-23
7
824 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:dloebig
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Rick_Rickards
Comment Utility
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

0
 

Author Comment

by:dloebig
Comment Utility
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"

0
 
LVL 16

Expert Comment

by:Rick_Rickards
Comment Utility
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:dloebig
Comment Utility
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.  

0
 
LVL 16

Accepted Solution

by:
Rick_Rickards earned 500 total points
Comment Utility
Usually one would set focus by entering something like this...

Me.IMR-Operator.SetFocus

Rick
0
 

Author Comment

by:dloebig
Comment Utility
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!
0
 
LVL 16

Expert Comment

by:Rick_Rickards
Comment Utility
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now