Solved

Cancel unload on main form if subform generates error

Posted on 2006-06-23
7
837 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Rick_Rickards
ID: 16974171
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
ID: 16984392
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
ID: 16984447
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dloebig
ID: 16984622
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
ID: 16984655
Usually one would set focus by entering something like this...

Me.IMR-Operator.SetFocus

Rick
0
 

Author Comment

by:dloebig
ID: 16985541
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
ID: 16985557
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

740 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