Solved

Cancel unload on main form if subform generates error

Posted on 2006-06-23
7
829 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

Back Up Your Microsoft Windows Server®

Back up 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.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
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…

778 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