Solved

Cancel unload on main form if subform generates error

Posted on 2006-06-23
7
826 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
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

932 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

16 Experts available now in Live!

Get 1:1 Help Now