Remove "Close Action was cancelled message"

G

Guest

I am using the OnUnload event on a form to trap and error. Everything works
fine but I would like to turn off the Access msg, "The Close Action was
cancelled." How can I do that?
 
D

Dirk Goldgar

bjnova said:
I am using the OnUnload event on a form to trap and error.
Everything works fine but I would like to turn off the Access msg,
"The Close Action was cancelled." How can I do that?

That error message would be coming from the code where you tried to
close the form -- probably by calling DoCmd.Close. You need to put
error-handling logic in that code, trap error 2501, and ignore it. For
example,

Private Sub cmdClose_Click()

On Error GoTo Err_Handler

DoCmd.Close acForm, Me.Name, acSaveNo

Exit_Point:
Exit Sub

Err_Handler:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End If
Resume Exit_Point

End Sub
 
G

Guest

Thanks Dirk, but for some reason this code is not working. What is happening
is that my homegrown message is being shown twice and then the "Close Action
was cancelled message" appears.

My code (including your recommendations):

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Handler

If (Me!chkPositive = True) And IsNull(cboteamMember) Then
MsgBox ("You have marked this record positive; please complete the
Initiating Team Member Field")
Me!cboteamMember.SetFocus
Cancel = True

'Else

'MsgBox ("Negative ...")
End If


Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End If
Resume Exit_Point

End Sub
 
G

Guest

One slight change: in the error trap I tried both

If Err.Number = 2501 and

If Err.Number <> 2501 (as you recommended)

No differernce in behavior.
 
D

Dirk Goldgar

bjnova said:
Thanks Dirk, but for some reason this code is not working. What is
happening is that my homegrown message is being shown twice and then
the "Close Action was cancelled message" appears.

My code (including your recommendations):

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Handler

If (Me!chkPositive = True) And IsNull(cboteamMember) Then
MsgBox ("You have marked this record positive; please
complete the Initiating Team Member Field")
Me!cboteamMember.SetFocus
Cancel = True

'Else

'MsgBox ("Negative ...")
End If


Exit_Point:
Exit Sub

Err_Handler:
If Err.Number = 2501 Then
MsgBox Err.Description, vbExclamation, "Error " &
Err.Number End If
Resume Exit_Point

End Sub

No, you misunderstood me. The trap for error 2501 doesn't go where you
cancel the event, it goes where you asked (in code) for the event to
occur. That is, it goes at the point where you have code that closes
the form, as for example with some variation of the statement

DoCmd.Close

That's where the error will be raised.

Incidentally, in the error-handler you want to display the error message
only when the error number is *not* 2501, so change your "=" back to
"<>", the way I posted it.
 
G

Guest

Thanks Dirk! It worked perfectly.

Dirk Goldgar said:
No, you misunderstood me. The trap for error 2501 doesn't go where you
cancel the event, it goes where you asked (in code) for the event to
occur. That is, it goes at the point where you have code that closes
the form, as for example with some variation of the statement

DoCmd.Close

That's where the error will be raised.

Incidentally, in the error-handler you want to display the error message
only when the error number is *not* 2501, so change your "=" back to
"<>", the way I posted it.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top