Handle Run-time Error 2501 when Print Form cancelled (1 Viewer)

connie

Registered User.
Local time
Today, 09:22
Joined
Aug 6, 2009
Messages
92
Hello,

I searched this forum and Google looking for a way to do this, but everyone's situation I found was unique enough for it not to work for me.

I have forms that will need to be printed by the user at times. I have used VBA code behind a "Print" button on the form. Here is my code:

Code:
Private Sub cmdPrintMapArea1_Click()
    DoCmd.RunCommand acCmdPrint
End Sub

It works fine unless:

1) User clicks the "Cancel" button on the print window if they change their mind...they then get "Run-time error '2501': The RunCommand action was canceled" with options to End, Debug, or go to Help. I don't want the users to get this confusing error, so I'd like to "handle" error 2501 so the user doesn't see this...maybe instead a message box that says "You have cancelled this print job" and only an "OK" button.

2) If the user clicks "Setup" on the print window, they get "This action can't be carried otu while processing a form or report event. A macro specified as the OnOpen, OnLoad, OnClose, OnFormat, OnRetreat, OnPage, or OnPrint property setting contains an invalid action for the property. When you click OK, an Action Failed dialog box will display the name of the macro that failed and its arguments." (No Action Failed dialog box comes up.)

Any help would be much appreciated! Thank you!
 

SimonB1978

Registered User.
Local time
Today, 09:22
Joined
Jan 22, 2009
Messages
161
Hi,

1) is easy to correct:
Code:
Private Sub cmdPrintMapArea1_Click()
On Error GoTo cmdPrintMapArea1_Click_Err
    DoCmd.RunCommand acCmdPrint
    Exit Sub
 
cmdPrintMapArea1_Click_Err:
 
    If Err.Number = 2501 Then
        MsgBox "You cancelled.....", vbOKOnly
    Else
        ' If you want to do something about other errors...
    End If
 
End Sub
As for 2) I'll leave this one to others...
 
Last edited:

connie

Registered User.
Local time
Today, 09:22
Joined
Aug 6, 2009
Messages
92
Thanks, Simon...that was exactly what I was trying to do for #1 :)
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 13:22
Joined
Sep 12, 2006
Messages
15,614
to generally see what the error is, in the error handler temporarily put

msgbox "Error: " & err & " Desc: " & err.description

this will show you error 2501 (probably the most common trappable error) - it will also show you the error number corresponding with your other situation

then you can use Simon's technique to handle all the errors

maybe try case instead - this sort of thing

select case err
case 2501: 'do nothing
case whatever: suitable message
case else: unexpected message
end select
 

connie

Registered User.
Local time
Today, 09:22
Joined
Aug 6, 2009
Messages
92
to generally see what the error is, in the error handler temporarily put

msgbox "Error: " & err & " Desc: " & err.description

this will show you error 2501 (probably the most common trappable error) - it will also show you the error number corresponding with your other situation

then you can use Simon's technique to handle all the errors

maybe try case instead - this sort of thing

select case err
case 2501: 'do nothing
case whatever: suitable message
case else: unexpected message
end select

Thanks. Well, the thing is it won't allow the user to enter Print Setup at all...so I wouldn't want to just handle that error, I'd like to fix it so the user can use the print window as normal.

So if I understand this correctly, to trap that error and see what precisely it is I should modify my code to:

Code:
Private Sub cmdPrintMapArea1_Click()
On Error GoTo cmdPrintMapArea1_Click_Err
    DoCmd.RunCommand acCmdPrint
    Exit Sub
 
cmdPrintMapArea1_Click_Err:
 
    If Err.Number = 2501 Then
        MsgBox "You cancelled.....", vbOKOnly
    Else
        [B]msgbox "Error: " & err & " Desc: " & err.description[/B]
    End If
 
End Sub
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 13:22
Joined
Sep 12, 2006
Messages
15,614
yes

this statement says that if there is an error, than jump to the indicated label

On Error GoTo cmdPrintMapArea1_Click_Err

then you are programming the label code to do what you want


--------
error 2051 is returned if a form or report doesnt open - this could be because there is no data, and you are preventing the from/report opening with code, when there is no data

but error 2051 also occurs if there is any error preventing the object opening - which could be a code bug, a type mismatch in the recordsource query etc etc.

--------
so this statement shows you some error details - if there is no syntax error, it will capitalise, and the layout will adjust

msgbox "Error: " & err & " Desc: " & err.description
 

boblarson

Smeghead
Local time
Today, 06:22
Joined
Jan 12, 2001
Messages
32,059
--------
error 2051 is returned if a form or report doesnt open - this could be because there is no data, and you are preventing the from/report opening with code, when there is no data

but error 2051 also occurs if there is any error preventing the object opening - which could be a code bug, a type mismatch in the recordsource query etc etc.
A bit dyslexic today, are we? :D It is error 2501, not 2051.
 

connie

Registered User.
Local time
Today, 09:22
Joined
Aug 6, 2009
Messages
92
Okay, I used that code, but just to clarify: I was getting 2501 only when user attempted to Cancel the print operation, and that was fixed by first poster's suggestion; it now (and still, after modifying code per my last reply) gives the message "You have successfully cancelled this print operation" which is what I wanted.

As for the other error, though, that's still occurring. It happens when you click on "Setup" in the print window. The error handling code isn't changing the message. Message attached via JPEG...
 

Attachments

  • error.jpg
    error.jpg
    96.4 KB · Views: 403
  • where-I-clicked-to-get-error.jpg
    where-I-clicked-to-get-error.jpg
    22.7 KB · Views: 366

Users who are viewing this thread

Top Bottom