Access97 - Close Form?

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
LVL 1
SHardyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
simonbennettConnect With a Mentor Commented:
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
 
dovholukCommented:
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
 
PaurthsCommented:
u can also just use
err.clear

cheers
Ricky
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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

dovholuk
0
 
simonbennettCommented:
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
 
dovholukCommented:
simon,

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

dovholuk
0
 
simonbennettCommented:
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
 
dovholukCommented:
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
 
simonbennettCommented:
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
 
SHardyAuthor Commented:
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
 
SHardyAuthor Commented:
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
 
simonbennettCommented:
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
 
PaurthsCommented:
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
 
dovholukCommented:
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
 
SHardyAuthor Commented:
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
 
simonbennettCommented:
<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
 
SHardyAuthor Commented:
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
 
simonbennettCommented:
No problem

Good luck all
0
All Courses

From novice to tech pro — start learning today.