473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Run time error 2501

31
Hi, I have an option group to select different reports, each report is based off a query that asks for criteria for that report. I have no problem opening any the querys through the group regardless of the data involved. The problem arises when you go to open a report and click the cancel button on the criteria question. I get a "Run Time Error 2501", "The Open Report Action was canceled". The error occurs only on the reoprt in the elseif statement. The grpreport = 6 opens other forms that have the criteria on the form, because there are 14 different criterias the query looks at. The grprrport = 6 also uses a macro for that specific report. The code I used is as to open the reports is as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdreport_Click()
  2. On Error GoTo Err_cmbreport_Click
  3.  
  4.  Dim stDocName As String
  5.  Dim stLinkCriteria As String
  6.  
  7.  
  8.  
  9. If Me.grpreports = 6 Then
  10.  
  11.     DoCmd.Close
  12.  
  13.     stDocName = "design type"
  14.     DoCmd.OpenForm stDocName, , , stLinkCriteria
  15.  
  16. ElseIf Me.grpreports = 1 Then
  17.  
  18.     DoCmd.Close
  19.  
  20.     DoCmd.OpenReport "raw data for a bar", acViewReport
  21.  
  22.  
  23. Exit_cmbreport_Click:
  24.  Exit Sub
  25.  
  26.     If Err.Number = 2501 Then
  27.         Resume Next
  28.     Else
  29. Err_cmbreport_Click:
  30.         MsgBox Err.Description
  31.         Resume Exit_cmbreport_Click
  32.    End If
  33.  
  34. End If
  35.  
  36.  
  37. End Sub
The program stops at the DoCmd.openreport line. I won't added the rest of the option buttons for reports untill I get past this problem. Can anyone help me capture the error?
Jun 3 '10 #1

✓ answered by patjones

I think the way for you to solve this is to determine which set of criteria you need some other way - say by entering it on the form, then passing that criteria to the report using the filter argument of OpenReport. This is generally how a report is filtered.

As a simple example of how this works, suppose I have a table called tblEmployees, with fields fldEmplID, fldLastName, fldFirstName, fldDateOfBirth.

In the Open event of a report based on this table, I can write, very simply:

Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = "SELECT * FROM tblEmployees"

This will pick everything from the table. But what if you want to restrict the results to everyone with the last name "Smith"? When you call the report from the form, you can use the Filter argument of the OpenReport call:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldLastName = 'Smith'"

Or what about everyone with a date of birth in 1985?

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldDateOfBirth BETWEEN #12/31/1984# AND #1/1/1986#"

As you can see, the filter criteria is passed in as a string, and it's not hard to see how you can call the report with different criteria depending on some other condition...like in your case - if option button 3 is selected you'll use one filter, but if option button 4 is selected you'll use a different filter.

It might take a little more coding on your part to make this work, but in the long run it will pay for you to organize it like this, I think.

Pat

8 9957
patjones
931 Expert 512MB
I think some of the lines in your code were out of order. Try this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdreport_Click()
  2.  
  3. On Error GoTo Err_cmdreport_Click
  4.  
  5. Dim stDocName As String
  6. Dim stLinkCriteria As String
  7.  
  8. If Me.grpreports = 6 Then
  9.  
  10.    DoCmd.Close
  11.    stDocName = "design type"
  12.    DoCmd.OpenForm stDocName, , , stLinkCriteria
  13.  
  14. ElseIf Me.grpreports = 1 Then
  15.  
  16.    DoCmd.Close
  17.    DoCmd.OpenReport "raw data for a bar", acViewReport
  18.  
  19. End If
  20.  
  21. Exit_cmdreport_Click:
  22.  
  23.    Exit Sub
  24.  
  25. Err_cmdreport_Click:
  26.  
  27.    If Err.Number = 2501 Then
  28.       Resume Next
  29.    Else
  30.       MsgBox Err.Description
  31.       Resume Exit_cmdreport_Click
  32.    End If
  33.  
  34. End Sub

You had the test for error 2501 before the GoTo label for subroutine error handling. This means that when the error occurs, it will skip past your test and display the error anyway.

Also, I think you put your last End If too far to the bottom of the subroutine, so I moved it up before the GoTo labels for Exit and Error handling (which are usually the last things to appear in a subroutine).

Finally, you had your GoTo labels "cmb" instead of "cmd"...

Pat
Jun 3 '10 #2
tomric
31
Thank you, but I'm still getting the "Run Time Error 2501". Have any other sugestions? I put a break in at the error sub routine and steped through it, it doesn't go into the routine before I get the error.
Jun 3 '10 #3
patjones
931 Expert 512MB
I would suggest putting a break somewhere near the top of the subroutine and stepping through it from that point to see exactly where the error is raised.

Pat
Jun 3 '10 #4
tomric
31
I tried that, it doesn't go into the subroutine. If I go through and run the report it works with no problems. It's just when I hit cancel it stops at the DoCmd.OpenReport line. I also tried steping through from the begining of the procedure, it gets to that line, opens the report which opens a query, hit cancel from the criteria question, goes back to that line and stops with the error.
Jun 3 '10 #5
patjones
931 Expert 512MB
But if you enter criteria and continue then the report opens correctly?

I think the problem is that when you hit "Cancel", the query is not getting parameters that it needs to in order to construct a record source for the report. As a result the report fails to open.

Pat
Jun 3 '10 #6
tomric
31
Yes that is correct, the report will run just fine. it's the cancel button that's the problem. If I try running the report by itself, with out the code from the form, and click cancel it works ok.
Jun 3 '10 #7
patjones
931 Expert 512MB
I think the way for you to solve this is to determine which set of criteria you need some other way - say by entering it on the form, then passing that criteria to the report using the filter argument of OpenReport. This is generally how a report is filtered.

As a simple example of how this works, suppose I have a table called tblEmployees, with fields fldEmplID, fldLastName, fldFirstName, fldDateOfBirth.

In the Open event of a report based on this table, I can write, very simply:

Expand|Select|Wrap|Line Numbers
  1. Me.RecordSource = "SELECT * FROM tblEmployees"

This will pick everything from the table. But what if you want to restrict the results to everyone with the last name "Smith"? When you call the report from the form, you can use the Filter argument of the OpenReport call:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldLastName = 'Smith'"

Or what about everyone with a date of birth in 1985?

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldDateOfBirth BETWEEN #12/31/1984# AND #1/1/1986#"

As you can see, the filter criteria is passed in as a string, and it's not hard to see how you can call the report with different criteria depending on some other condition...like in your case - if option button 3 is selected you'll use one filter, but if option button 4 is selected you'll use a different filter.

It might take a little more coding on your part to make this work, but in the long run it will pay for you to organize it like this, I think.

Pat
Jun 4 '10 #8
tomric
31
Thank you for your help.
Jun 4 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ColinWard | last post by:
My previous message was blank so Im trying again. I have a button on a form which opens the Import dialogue box. This works fine except if I click on the "X" to close the form I get Run-Time Error...
0
by: mondo3 | last post by:
Someone had previously created an access 2000 database (tables and forms together in a single .mdb file). The .mdb file was located on a server, and two users (on two separate computers) share...
1
by: Bob Dydd | last post by:
Hi everyone It's me again. I have an access 2000 database with 12 landscape reports which sometimes have to be FAXED and other times printed, so I have written the following code and put it...
5
by: fearblanco | last post by:
Hello - I am receiving the below error message when attempting to open a report. This database is used by approximately 20 users and only one user is having this problem (even I can't duplicate...
4
by: Keith | last post by:
I have the following code in the On No Data event of a report: **** On Error GoTo err_trap MsgBox "No items matching criteria.", vbInformation, gcApplication Cancel = True err_trap: If...
1
by: Don | last post by:
I have a form that uses an Image object to open a report (the report is based on a query). I used the image object because the MS command buttons are dull looking. All of this works well. The...
3
by: fperri | last post by:
Hello, I have a form in Access for users to do updates from. I'm trying to catch the error 2501 that pops up if they say cancel to the prompt that tells them how many records they are going to...
1
by: Haole88 | last post by:
I am brand new to VBA - so if this is a brain-donor question...sorry!! I have a search button on my switchboard with two fields next to it - Status and Technician. I can search for both fields...
1
by: Craig2000 | last post by:
My module will run fine, but when I want to open a report that's not populated I get the error. (As if the end user made a mistake) Here's an example of the code. ' Prints EE List ...
7
sassy2009
by: sassy2009 | last post by:
Hello, I am running an insert query from xl spreadsheet using the DoCmd.RunSQL to insert values from the spreadsheet into the Access database. When i run this query it gives an error saying "...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.