Can't cancel an automatic Access message

N

night_writer

I hope someone can help me. I've found two different solutions in
various newsgroups, but neither seems to work.

Upon exiting a form, I want to check that one and only of the
underlying records has a yes/no field set to -1. If that condition is
not met, I provide an appropriate error message and cancel the closing
of the form, so the user can fix the problem. My problem is that in
addition to my message, access adds a message that says: "You
cancelled the previous operation."

I have tried using DoCmd.SetWarnings before and after the cancel event
like this:

DoCmd.SetWarnings False
DoCmd.CancelEvent
DoCmd.SetWarnings True

It doesn't appear to have any effect. Then I found a posting that said
this was the result of error 2501, so I added a trap for that to the
OnError section, but that seems to have no effect either, including
the fact that I don't get any error number at all on the message box
that says "You cancelled the previous operation." So maybe this isn't
actually an error message? Anyway, my code is as below. I hope someone
can hope me remove the annoying extra message.

~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload

Dim intDefaults As Integer

intDefaults = DCount("[strCompanyAbbrev]", "tlkpCompany", "[ynDefault]
= -1")

If intDefaults = 0 Then
MsgBox "You must select one company as the default."
DoCmd.CancelEvent
ElseIf intDefaults > 1 Then
MsgBox "There can be only one default company."
DoCmd.CancelEvent
End If

Exit_Form_Unload:
Exit Sub

Err_Form_Unload:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
End If
Resume Exit_Form_Unload

End Sub
~~~~~~~~~~~~~~
Thank you for your assistance!
Alice
 
M

Marshall Barton

night_writer said:
I hope someone can help me. I've found two different solutions in
various newsgroups, but neither seems to work.

Upon exiting a form, I want to check that one and only of the
underlying records has a yes/no field set to -1. If that condition is
not met, I provide an appropriate error message and cancel the closing
of the form, so the user can fix the problem. My problem is that in
addition to my message, access adds a message that says: "You
cancelled the previous operation."

I have tried using DoCmd.SetWarnings before and after the cancel event
like this:

DoCmd.SetWarnings False
DoCmd.CancelEvent
DoCmd.SetWarnings True

It doesn't appear to have any effect. Then I found a posting that said
this was the result of error 2501, so I added a trap for that to the
OnError section, but that seems to have no effect either, including
the fact that I don't get any error number at all on the message box
that says "You cancelled the previous operation." So maybe this isn't
actually an error message? Anyway, my code is as below. I hope someone
can hope me remove the annoying extra message.

~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload

Dim intDefaults As Integer

intDefaults = DCount("[strCompanyAbbrev]", "tlkpCompany", "[ynDefault]
= -1")

If intDefaults = 0 Then
MsgBox "You must select one company as the default."
DoCmd.CancelEvent
ElseIf intDefaults > 1 Then
MsgBox "There can be only one default company."
DoCmd.CancelEvent
End If

Exit_Form_Unload:
Exit Sub

Err_Form_Unload:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
End If
Resume Exit_Form_Unload

End Sub


That message comes from the procedure with the DoCmd.Close
indicating that the Close was unsuccessful. Put the error
handling code in that procedure.
 
N

night_writer

night_writer said:
I hope someone can help me. I've found two different solutions in
various newsgroups, but neither seems to work.
Upon exiting a form, I want to check that one and only of the
underlying records has a yes/no field set to -1. If that condition is
not met, I provide an appropriate error message and cancel the closing
of the form, so the user can fix the problem. My problem is that in
addition to my message, access adds a message that says: "You
cancelled the previous operation."
I have tried using DoCmd.SetWarnings before and after the cancel event
like this:
     DoCmd.SetWarnings False
     DoCmd.CancelEvent
     DoCmd.SetWarnings True
It doesn't appear to have any effect. Then I found a posting that said
this was the result of error 2501, so I added a trap for that to the
OnError section, but that seems to have no effect either, including
the fact that I don't get any error number at all on the message box
that says "You cancelled the previous operation." So maybe this isn't
actually an error message? Anyway, my code is as below. I hope someone
can hope me remove the annoying extra message.
~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
Dim intDefaults As Integer
intDefaults = DCount("[strCompanyAbbrev]", "tlkpCompany", "[ynDefault]
= -1")
If intDefaults = 0 Then
   MsgBox "You must select one company as the default."
   DoCmd.CancelEvent
ElseIf intDefaults > 1 Then
   MsgBox "There can be only one default company."
   DoCmd.CancelEvent
End If
Exit_Form_Unload:
   Exit Sub
Err_Form_Unload:
   If Err.Number <> 2501 Then
     MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
   End If
   Resume Exit_Form_Unload

That message comes from the procedure with the DoCmd.Close
indicating that the Close was unsuccessful.  Put the error
handling code in that procedure.

--
Marsh
MVP [MS Access]- Hide quoted text -

- Show quoted text -

Thank you for the response, but I don't understand how to do this.

I tried creating a new event procedure for OnClose:
~~~~~~~~~
Private Sub Form_Close()
On Error GoTo Err_Form_Close

Exit_Form_Close:
Exit Sub

Err_Form_Close:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
End If
Resume Exit_Form_Close

End Sub
~~~~~~~~~

But it has no effect. I also tried adding the error trapping to my
btnClose_Click procedure, but also no luck. I never actually type in a
DoCmd.Close, and I don't understand how to get to it.

I also think there might be something wrong with the error trapping
code itself, since I never get an error number on the message box that
appears.

I would appreciate any additional clarification you can provide.
Thanks!
Alice
 
M

Marshall Barton

night_writer said:
night_writer said:
I hope someone can help me. I've found two different solutions in
various newsgroups, but neither seems to work.
Upon exiting a form, I want to check that one and only of the
underlying records has a yes/no field set to -1. If that condition is
not met, I provide an appropriate error message and cancel the closing
of the form, so the user can fix the problem. My problem is that in
addition to my message, access adds a message that says: "You
cancelled the previous operation."
I have tried using DoCmd.SetWarnings before and after the cancel event
like this:
     DoCmd.SetWarnings False
     DoCmd.CancelEvent
     DoCmd.SetWarnings True
It doesn't appear to have any effect. Then I found a posting that said
this was the result of error 2501, so I added a trap for that to the
OnError section, but that seems to have no effect either, including
the fact that I don't get any error number at all on the message box
that says "You cancelled the previous operation." So maybe this isn't
actually an error message? Anyway, my code is as below. I hope someone
can hope me remove the annoying extra message.
~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
Dim intDefaults As Integer
intDefaults = DCount("[strCompanyAbbrev]", "tlkpCompany", "[ynDefault]
= -1")
If intDefaults = 0 Then
   MsgBox "You must select one company as the default."
   DoCmd.CancelEvent
ElseIf intDefaults > 1 Then
   MsgBox "There can be only one default company."
   DoCmd.CancelEvent
End If
Exit_Form_Unload:
   Exit Sub
Err_Form_Unload:
   If Err.Number <> 2501 Then
     MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
   End If
   Resume Exit_Form_Unload

That message comes from the procedure with the DoCmd.Close
indicating that the Close was unsuccessful.  Put the error
handling code in that procedure.

Thank you for the response, but I don't understand how to do this.

I tried creating a new event procedure for OnClose:
~~~~~~~~~
Private Sub Form_Close()
On Error GoTo Err_Form_Close

Exit_Form_Close:
Exit Sub

Err_Form_Close:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
End If
Resume Exit_Form_Close

End Sub
~~~~~~~~~

But it has no effect. I also tried adding the error trapping to my
btnClose_Click procedure, but also no luck. I never actually type in a
DoCmd.Close, and I don't understand how to get to it.

I also think there might be something wrong with the error trapping
code itself, since I never get an error number on the message box that
appears.

If you are not using DoCmd.Close, then users are expected to
close the form via the X button or some other built in
mechanism. In this case, the message is notifying the user
that the form could not be closed and you can not prevent
it.

For those reasons, it is common to either leave things alone
because it it the way Access works or to remove the X button
(CloseButton property) and use a command button with
DoCmd.Close along with the error handling code and your own
message.
 
N

night_writer

night_writer said:
night_writer wrote:
I hope someone can help me. I've found two different solutions in
various newsgroups, but neither seems to work.
Upon exiting a form, I want to check that one and only of the
underlying records has a yes/no field set to -1. If that condition is
not met, I provide an appropriate error message and cancel the closing
of the form, so the user can fix the problem. My problem is that in
addition to my message, access adds a message that says: "You
cancelled the previous operation."
I have tried using DoCmd.SetWarnings before and after the cancel event
like this:
     DoCmd.SetWarnings False
     DoCmd.CancelEvent
     DoCmd.SetWarnings True
It doesn't appear to have any effect. Then I found a posting that said
this was the result of error 2501, so I added a trap for that to the
OnError section, but that seems to have no effect either, including
the fact that I don't get any error number at all on the message box
that says "You cancelled the previous operation." So maybe this isn't
actually an error message? Anyway, my code is as below. I hope someone
can hope me remove the annoying extra message.
~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
Dim intDefaults As Integer
intDefaults = DCount("[strCompanyAbbrev]", "tlkpCompany", "[ynDefault]
= -1")
If intDefaults = 0 Then
   MsgBox "You must select one company as the default."
   DoCmd.CancelEvent
ElseIf intDefaults > 1 Then
   MsgBox "There can be only one default company."
   DoCmd.CancelEvent
End If
Exit_Form_Unload:
   Exit Sub
Err_Form_Unload:
   If Err.Number <> 2501 Then
     MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
   End If
   Resume Exit_Form_Unload
End Sub
That message comes from the procedure with the DoCmd.Close
indicating that the Close was unsuccessful.  Put the error
handling code in that procedure.
Thank you for the response, but I don't understand how to do this.
I tried creating a new event procedure for OnClose:
~~~~~~~~~
Private Sub Form_Close()
On Error GoTo Err_Form_Close
Exit_Form_Close:
   Exit Sub
Err_Form_Close:
   If Err.Number <> 2501 Then
     MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
   End If
   Resume Exit_Form_Close
End Sub
~~~~~~~~~
But it has no effect. I also tried adding the error trapping to my
btnClose_Click procedure, but also no luck. I never actually type in a
DoCmd.Close, and I don't understand how to get to it.
I also think there might be something wrong with the error trapping
code itself, since I never get an error number on the message box that
appears.

If you are not using DoCmd.Close, then users are expected to
close the form via the X button or some other built in
mechanism.  In this case, the message is notifying the user
that the form could not be closed and you can not prevent
it.

For those reasons, it is common to either leave things alone
because it it the way Access works or to remove the X button
(CloseButton property) and use a command button with
DoCmd.Close along with the error handling code and your own
message.

--
Marsh
MVP [MS Access]- Hide quoted text -

- Show quoted text -

Thank you for the additional comments, but we seem to be going in
circles. I am using DoCmd.Close as the procedure for my btnClose_Click
event (btnClose being a control I added to the form), and the error
trapping doesn't work there either. I have tried several things to
make this work, including (also) attaching the code to the OnClose
event of the form.

I can't believe nobody else ever wanted to cancel a form closure with
their own message box and surpress the Access one. I think I'm missing
something simple, and I think the fact that I'm not getting any
err.number in my message box even though the error trapping code
explicitly calls for one may be significant, like perhaps the code
just isn't getting called, or is ignored, or is in the wrong place.

I would apprecaite any further help you can give me, but if you want
to give up, please let me know, and I'll start another thread
specifically asking for help on the error trapping.

Thanks.
Alice
 
M

Marshall Barton

night_writer said:
night_writer said:
night_writer wrote:
I hope someone can help me. I've found two different solutions in
various newsgroups, but neither seems to work.
Upon exiting a form, I want to check that one and only of the
underlying records has a yes/no field set to -1. If that condition is
not met, I provide an appropriate error message and cancel the closing
of the form, so the user can fix the problem. My problem is that in
addition to my message, access adds a message that says: "You
cancelled the previous operation."
I have tried using DoCmd.SetWarnings before and after the cancel event
like this:
     DoCmd.SetWarnings False
     DoCmd.CancelEvent
     DoCmd.SetWarnings True
It doesn't appear to have any effect. Then I found a posting that said
this was the result of error 2501, so I added a trap for that to the
OnError section, but that seems to have no effect either, including
the fact that I don't get any error number at all on the message box
that says "You cancelled the previous operation." So maybe this isn't
actually an error message? Anyway, my code is as below. I hope someone
can hope me remove the annoying extra message.
~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
Dim intDefaults As Integer
intDefaults = DCount("[strCompanyAbbrev]", "tlkpCompany", "[ynDefault]
= -1")
If intDefaults = 0 Then
   MsgBox "You must select one company as the default."
   DoCmd.CancelEvent
ElseIf intDefaults > 1 Then
   MsgBox "There can be only one default company."
   DoCmd.CancelEvent
End If
Exit_Form_Unload:
   Exit Sub
Err_Form_Unload:
   If Err.Number <> 2501 Then
     MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
   End If
   Resume Exit_Form_Unload
That message comes from the procedure with the DoCmd.Close
indicating that the Close was unsuccessful.  Put the error
handling code in that procedure.
Thank you for the response, but I don't understand how to do this.
I tried creating a new event procedure for OnClose:
~~~~~~~~~
Private Sub Form_Close()
On Error GoTo Err_Form_Close
Exit_Form_Close:
   Exit Sub
Err_Form_Close:
   If Err.Number <> 2501 Then
     MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
   End If
   Resume Exit_Form_Close
End Sub
~~~~~~~~~
But it has no effect. I also tried adding the error trapping to my
btnClose_Click procedure, but also no luck. I never actually type in a
DoCmd.Close, and I don't understand how to get to it.
I also think there might be something wrong with the error trapping
code itself, since I never get an error number on the message box that
appears.

If you are not using DoCmd.Close, then users are expected to
close the form via the X button or some other built in
mechanism.  In this case, the message is notifying the user
that the form could not be closed and you can not prevent
it.

For those reasons, it is common to either leave things alone
because it it the way Access works or to remove the X button
(CloseButton property) and use a command button with
DoCmd.Close along with the error handling code and your own
message.

Thank you for the additional comments, but we seem to be going in
circles. I am using DoCmd.Close as the procedure for my btnClose_Click
event (btnClose being a control I added to the form), and the error
trapping doesn't work there either. I have tried several things to
make this work, including (also) attaching the code to the OnClose
event of the form.

I can't believe nobody else ever wanted to cancel a form closure with
their own message box and surpress the Access one. I think I'm missing
something simple, and I think the fact that I'm not getting any
err.number in my message box even though the error trapping code
explicitly calls for one may be significant, like perhaps the code
just isn't getting called, or is ignored, or is in the wrong place.

I would apprecaite any further help you can give me, but if you want
to give up, please let me know, and I'll start another thread
specifically asking for help on the error trapping.

The code in your close the form command button's Click event
procedure should look like:

Sub mybutton_Click()
On Error GoTo CatchErrors
DoCmd.Close acForm, Me.Name, acSaveNo

Done:
   Exit Sub

CatchErrors:
   If Err.Number = 2501 Then
MesgBox "something is wrong", ."Can't close"
Else
     MsgBox Err.Description, vbExclamation, "Error #: " &
Err.Number
   End If
   Resume Done
End Sub

Also make sure the button's OnClick property is set to:
[Event Procedure]

When you test that code, check your message box to make sure
you are getting error 2501.

If you are not getting either message, then there may be
some other error confusing the issue. Try using the form's
Error event to see if you get any clues.
 
N

night_writer

night_writer said:
night_writer wrote:
night_writer wrote:
I hope someone can help me. I've found two different solutions in
various newsgroups, but neither seems to work.
Upon exiting a form, I want to check that one and only of the
underlying records has a yes/no field set to -1. If that conditionis
not met, I provide an appropriate error message and cancel the closing
of the form, so the user can fix the problem. My problem is that in
addition to my message, access adds a message that says: "You
cancelled the previous operation."
I have tried using DoCmd.SetWarnings before and after the cancel event
like this:
DoCmd.SetWarnings False
DoCmd.CancelEvent
DoCmd.SetWarnings True
It doesn't appear to have any effect. Then I found a posting that said
this was the result of error 2501, so I added a trap for that to the
OnError section, but that seems to have no effect either, including
the fact that I don't get any error number at all on the message box
that says "You cancelled the previous operation." So maybe this isn't
actually an error message? Anyway, my code is as below. I hope someone
can hope me remove the annoying extra message.
~~~~~~~~~
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_Form_Unload
Dim intDefaults As Integer
intDefaults = DCount("[strCompanyAbbrev]", "tlkpCompany", "[ynDefault]
= -1")
If intDefaults = 0 Then
MsgBox "You must select one company as the default."
DoCmd.CancelEvent
ElseIf intDefaults > 1 Then
MsgBox "There can be only one default company."
DoCmd.CancelEvent
End If
Exit_Form_Unload:
Exit Sub
Err_Form_Unload:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
End If
Resume Exit_Form_Unload
End Sub
That message comes from the procedure with the DoCmd.Close
indicating that the Close was unsuccessful. Put the error
handling code in that procedure.
Thank you for the response, but I don't understand how to do this.
I tried creating a new event procedure for OnClose:
~~~~~~~~~
Private Sub Form_Close()
On Error GoTo Err_Form_Close
Exit_Form_Close:
Exit Sub
Err_Form_Close:
If Err.Number <> 2501 Then
MsgBox Err.Description, vbExclamation, "Error #: " & Err.Number
End If
Resume Exit_Form_Close
End Sub
~~~~~~~~~
But it has no effect. I also tried adding the error trapping to my
btnClose_Click procedure, but also no luck. I never actually type in a
DoCmd.Close, and I don't understand how to get to it.
I also think there might be something wrong with the error trapping
code itself, since I never get an error number on the message box that
appears.
If you are not using DoCmd.Close, then users are expected to
close the form via the X button or some other built in
mechanism. In this case, the message is notifying the user
that the form could not be closed and you can not prevent
it.
For those reasons, it is common to either leave things alone
because it it the way Access works or to remove the X button
(CloseButton property) and use a command button with
DoCmd.Close along with the error handling code and your own
message.
Thank you for the additional comments, but we seem to be going in
circles. I am using DoCmd.Close as the procedure for my btnClose_Click
event (btnClose being a control I added to the form), and the error
trapping doesn't work there either. I have tried several things to
make this work, including (also) attaching the code to the OnClose
event of the form.
I can't believe nobody else ever wanted to cancel a form closure with
their own message box and surpress the Access one. I think I'm missing
something simple, and I think the fact that I'm not getting any
err.number in my message box even though the error trapping code
explicitly calls for one may be significant, like perhaps the code
just isn't getting called, or is ignored, or is in the wrong place.
I would apprecaite any further help you can give me, but if you want
to give up, please let me know, and I'll start another thread
specifically asking for help on the error trapping.

The code in your close the form command button's Click event
procedure should look like:

Sub mybutton_Click()
   On Error GoTo CatchErrors
   DoCmd.Close  acForm, Me.Name, acSaveNo

Done:
Exit Sub

CatchErrors:
If Err.Number =  2501 Then
      MesgBox "something is wrong", ."Can't close"
   Else
  MsgBox Err.Description, vbExclamation, "Error #: " &
Err.Number
End If
Resume Done
End Sub

Also make sure the button's OnClick property is set to:
   [Event Procedure]

When you test that code, check your message box to make sure
you are getting error 2501.  

If you are not getting either message, then there may be
some other error confusing the issue.  Try using the form's
Error event to see if you get any clues.

--
Marsh
MVP [MS Access]- Hide quoted text -

- Show quoted text -

Thank you (again) for your additional work on this. I am beginning to
believe this is hopeless. I tried adding your change to the error
trapping associated with my btnClose, but the code just isn't getting
executed, no matter where I put it. If it was getting executed, I
would get an Err.Number on the Access message, but I'm not getting
one, so that message is being created someplace else other than the
Unload or DoCmd.Cancel events, or Access doesn't consider it to be an
error (is that possible?).

I'm about ready to just make my users live with the extra message, but
that just seems way too defeatist. I will look at it again on Monday.
Perhaps a solution will magically appear in the back of my brain over
the weekend!

Thanks again for your efforts.
Alice
 

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