Solved

Access97 - Close Form?

Posted on 2001-06-21
18
363 Views
Last Modified: 2012-05-04
I have a form, in an Access97 database, for which I have assigned an "OnUnload" event handler. What it does is simply test for a condition, and if that condition is true then the form will go ahead and close. Else, the Unload event is cancelled, and the form remains open.

This works fine... except for one thing. When the condition isn't met, and the form remains open, I get a message telling me that "The Close Action Was Cancelled" (and then further information in the same dialogue box).

I don't want this message displayed. How do I stop it from appearing?

I had already used the DoCmd.SetWarnings method, but this doesn't seem to prevent it. Besides, I need to set the SetWarnings to true in the very last line anyway.

Can anyone help please?

Thanks,
Simon
0
Comment
Question by:SHardy
  • 7
  • 5
  • 4
  • +1
18 Comments
 
LVL 6

Accepted Solution

by:
simonbennett earned 50 total points
ID: 6214245
Hi

Are you using error handling code? If not, simply wrap the Docmd.Close in an errorless state, e.g.

    On Error Resume Next
    DoCmd.Close acForm, "FormName"
    On Error GoTo 0

This works fine in Access 2K

HTH

Simon
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6214330
here's how i'd handle it:

'********************************************************
if [some condition here] = false then
     msgbox "you can't close the form yet!"
     cancel = true
     exit sub
end if

msgbox "i'm closing the form now..."
'********************************************************

is this what your code looks like? are you setting Cancel to true in your onUnload event?

do you have a docmd.closeform() command in the onUnload event?

dovholuk
0
 
LVL 12

Expert Comment

by:Paurths
ID: 6214393
u can also just use
err.clear

cheers
Ricky
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6214488
err.clear won't set cancel to true. it will only reset the error object.

dovholuk
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6214535
You need to either

1)Ignore the error (see my first post)
2)Trap the error e.g.

on error goto errorhandler
docmd.close acform,"Formname"
exit sub

errorhandler:

' whatever errorhandler you like

end sub

Whichever way the error occurs at the docmd.Close code *and not* in the unload event.

Good Luck

Simon
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6214634
simon,

with the proper coding, there should never BE an error. thus no error needs to be handled or trapped.

dovholuk
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6214698
Surely the error is by "design" - i.e. cancelling the docmd.close will *always* raise an error. Try it. If you close on a button with

DoCmd.Close acForm, Me.Name

and set cancel to true in Form_Unload, VB generates the error

Run Time Error 2501
The close action was cancelled

That's just the way it is!

Simon
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6214772
ok, i see your point and where you're coming from. you're right, cancelling the docmd.close event will generate an error. however, canceling the docmd.close event is just plain silly. why would you EVER cancel the docmd.close event?!?!  if there is validation code that occurrs in the onUnload event, that code should be moved to whereever the docmd.close is located. then, if it passes the criteria, the docmd.close occurrs. if not, then nothing. programming "purists" consider it sloppy coding to handle "expected" errors. if an error can be anticipated, it can be tested for before the error occurs... *shrug*

however, i'd really like SHardy to get involved in the question so we know where we're going.

dovholuk
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6214860
Here Here - SHardy where are you?

And I completely agree - all validation should be processed before the close method is called. However we can't assume that the questioner is able to code that way - hence the solution to the immediate problem.

Regards

Simon
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.

 
LVL 1

Author Comment

by:SHardy
ID: 6214931
Only just received notification of postings to this question. It's always a bit slow in letting me know.

To answer some of the points raised:

1) It NEEDS to cancel the docmd.close on occassion. The form is being used to allocate cash received/paid against invoices. If the allocations do not balance, then I do not want to let the user leave the screen until they do balance.

2) I was coding to the OnUnload event because, even though there is a close button on the form that I could've coded to, there are other ways to close the form. I need to capture all the possible ways that the user could close the form. Hence using the OnUnload event.

3) My code does look similar to that presented by Dovholuk.

4) I do set Cancel to true in the OnUnload event. Else it would always close the form(?). It also seems that this is the reason for the message. It is telling me that the "close action has been cancelled".

5) I do not use the DoCmd.CloseForm action in the OnUnload event. If Cancel is not set to true, then the form is closed. The close command has already been called in order to trigger this event.

Hope this helps!
0
 
LVL 1

Author Comment

by:SHardy
ID: 6214957
Just another thought. The difference between the OnUnload and OnClose events is that the OnUnload can be cancelled, whereas the OnClose cannot.

Surely then, situations like this must be expected. So it would've been nice for them to stop any messages popping up. It's not even really an error message. It's just informing the user that close action has been cancelled. This is not needed. I have a message box to inform the user exactly why the form didn't close.
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6214960
Ok

Using Dovholuk's code, try

********************************************************
if [some condition here] = false then
    msgbox "you can't close the form yet!"
    on error reusme next    
    cancel = true
    on error goto 0
    exit sub
end if

msgbox "i'm closing the form now..."
'********************************************************

My last chance attempt.... ;-)

Simon
0
 
LVL 12

Expert Comment

by:Paurths
ID: 6214962
take it easy dovholuk.

<err.clear won't set cancel to true. it will only reset the error object>

thank u for pointing that out to me. I was not aware of that. I am a beginner at access.

<it will only reset the error object> <----> this is exactly what needs to be done.
0
 
LVL 8

Expert Comment

by:dovholuk
ID: 6214980
what you need to do is both validate before the docmd.close call behind the command button AND in the onUnload event.

validating the docmd.close event will handle the "close" button.

then, the OnUnload event will handle closing the form via the "x" in the upper right, OR the application closing.

by placing your validation in both spots, you'll never need to "catch" an error...

dovholuk
0
 
LVL 1

Author Comment

by:SHardy
ID: 6214997
Except that it is not strictly speaking an error.

Just testing further, noticed that you can close by the other methods without the message appearing (i.e. by the menus, or by the close button on the title bar). Therefore it only happens when cancelling the command launched from the custom close button.

I will now try Simon's original suggestion.

Back in a mo. :0)
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6214998
<whisper>Put your validation code in a separate function that returns a boolean, that way you just call the validator function on_close and on_click</whisper>
0
 
LVL 1

Author Comment

by:SHardy
ID: 6215061
We have a result!!!
And it was the very first comment.

Sorry, I always used the custom close button, and assumed that the other close methods used DoCmd.Close internally. Obviously they don't, as the message only appeared when using the custom button, upon further testing.

Only needed to change the code for the close button, as suggested in Simon's first comment.

Thanks Simon.

And thank you everyone else for giving it your time.
0
 
LVL 6

Expert Comment

by:simonbennett
ID: 6215173
No problem

Good luck all
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now