Trap run-time error in parameter report (1 Viewer)

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
In Access 2000, I use a tab control form with an option group on each page as a menu to run various reports, triggered with a command button. When I click the command button and then try to Cancel a report based on a parameter query, I get a MS VB box with "Run-time error '2501'. The OpenReport action was canceled."

I tried to trap this error in my error routine in the tab form (see code) but debug statements inserted at the start of the error handler are not executed and the code hangs at the OpenReport command. The report uses SQL as the record source, although I get the same error by using the parameter query itself as the source. Can anyone help me with this? Thanks.

Private Sub cmdPreviewRpt_Click()
On Error GoTo ErrorHandler

Dim strDocName As String
Dim tbc As Control, pge As Page, ctl As Control
Dim ThisDate As String, S As String

Set tbc = Me!TabCtl0
Set pge = tbc.Pages(tbc.Value)

If pge.Name = "Customer" Then
Select Case grpCustomer
Case 1
strDocName = "rptCustPast?Months"
DoCmd.OpenReport strDocName, acViewPreview
Case 2
<more code>
End Select
End If

Exit_ErrorHandler:
Exit Sub

ErrorHandler:
If Err.Number = 2501 Then
Dim Msg As String
Msg = "Error Number: " & Err.Number & ", Description: " & Err.Description
MsgBox (Msg)
Resume Exit_ErrorHandler
End If
End Sub
 
R

Rich

Guest
If Err.Number = 2501 Then
Resume Exit_ErrorHandler
Else
Dim Msg As String
Msg = "Error Number: " & Err.Number & ", Description: " & Err.Description
MsgBox (Msg)
Resume Exit_ErrorHandler
End If
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
Rich - Thanks for your post. I plugged this into my code but got the same result as before. It's as if the On Error line is ignored and Access goes right to its own standard error dialog.
Charles
 

Mile-O

Back once again...
Local time
Today, 14:16
Joined
Dec 10, 2002
Messages
11,316
For that error you need to use the form's Error event.
 

Mile-O

Back once again...
Local time
Today, 14:16
Joined
Dec 10, 2002
Messages
11,316
While my eyes are still young. ;)
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
Okay, I commented out the On Error statements in every Sub in the form and included the following (from Access Help) for the OnError event, but still got the Access message instead of mine.

Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conCancelParameterReport = 2051
Dim strMsg As String

If DataErr = conCancelParameterReport Then
Response = acDataErrContinue
strMsg = "You did not enter data needed to run the selected report."
MsgBox strMsg
End If
End Sub

I do appreciate the suggestions and hope someone can come up with the answer.
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
Sorry, that was a typo; I used 2501 in my test. And I'm beginning to wonder if my Access is corrupt. When I display the code for this form I show about 42% of System and User resources available; when I just click in the code without typing anything, resources plummet to 12%. Does that seem right? I have 384Mb RAM.
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
I started over with this problem and got some more clues but still no solution. I created a form with a command button that runs a report based on a parameter query, then I inserted a Debug statement for every relevant event for the form, button, and report. The Debug argument looks like, for example, "Form Button Click" & ", " & Err.Number.

Then I opened the form, clicked the button to display the parameter input box, entered no data and clicked the Cancel button. I immediately got a Microsoft Visual Basic box with "Run-time error '2501'. The OpenReport action was canceled." The box had Debug and End buttons.

I clicked on Debug and opened an Immediate window. Here's what my Debug statements showed:

Form Open, 0
Form Button Got Focus, 0
Form Button Click, 0
Report Open, 0
Report Close, 0
Form Button Lost Focus, 2501

I then went back and added a test for error 2501 in the command button's Lost Focus event and ran the test again. Still got the MS VB error box but, when I clicked Debug, got my own error msg from Lost Focus.

I even tried a "DoCmd.SetWarnings False" statement in the form's Open event, with the same result. Can it be that this error can't be trapped gracefully?
 

pdx_man

Just trying to help
Local time
Today, 06:16
Joined
Jan 23, 2001
Messages
1,347
I do similar stuff, but my error handler looks like:

HandleIt:
If Err.Number = 2501 Then Exit Sub
MsgBox "Error Number: " & Err.Number & ", Description: " & Err.Description

Your's says that if you have error 2501 then display the message. Try:
If Err.Number <> 2501 Then

Like what Rich has in his first post.
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
Hi PDX MAN,

Tried your suggestions but got the same result. I can trap the 2501 error but not before Access sees it and displays its own message. I'm using the code from Access Help in the OnError event for both the form and the report, where the statement "Response = acDataErrContinue" is supposed to suppress the Access default error message but obviously doesn't. Apparently you can't use this argument for other events (click, focus, etc.) Does anyone know of a comparable statement for those events? As time allows, I'm searching Google also but nothing so far. Thanks.
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
End of My Story (I guess)

Yesterday I stumbled across (literally) a page in Access Help that listed trappable errors for the Jet database engine. Errors were in the 2000-3999 range, and #2501 was not in the list.

Interestingly, I looked through Help again today and could find no reference that displayed this list. Does anyone know what to search for to pull it up again?
 

pdx_man

Just trying to help
Local time
Today, 06:16
Joined
Jan 23, 2001
Messages
1,347
My suggestion would be to try to re-create the DB simplified to a point where you can zip it and include the file here. The code I have above works for me. When I remove the error trapping, I get the message as you do. But when Include it, I am able to trap it. Try to post a sample DB where you are getting this error. That would help us tremendously.
 

ChrisO

Registered User.
Local time
Today, 23:16
Joined
Apr 30, 2003
Messages
3,202
Have a look in Options/Advanced/Error Trapping and see if it is set to Break on all errors.
If so, set it to Break in Class Module and try again.

Hope that helps.

Regards,
Chris.
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
Okay, I'll try to pull out relevant stuff and create a subset of the current 10Mb db. My time will be limited for a while; wife is scheduled for major surgery next week. Thanks for the suggestion.
 

CharlesHerrick

Registered User.
Local time
Today, 14:16
Joined
Oct 28, 2003
Messages
20
CHRIS! Your suggestion worked!

Making that simple change results in "The OpenReport action was canceled" message, with an OK to return to the calling form. That's all I need.

Many, many thanks to all of you for your assistance.

--Charles
 

sinecodap

New member
Local time
Today, 06:16
Joined
Aug 29, 2012
Messages
2
Got a solution to this....and it worked.. try below. Good luck all.

Private Sub View_Courses_Click()
On Error GoTo ErrorHandler
DoCmd.Close acForm, "ViewRecords", acSaveYes
DoCmd.OpenReport "Courses", acViewReport, , , acWindowNormal
Exit_ErrorHandler:
Exit Sub

ErrorHandler:
If Err.Number = 2501 Then
Dim Msg As String
Msg = Err.Description
MsgBox "You did not enter data needed to run the selected report", vbInformation, "Action Cancelled"
Resume Exit_ErrorHandler
End If
End Sub
 

Users who are viewing this thread

Top Bottom