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: - Private Sub cmdreport_Click()
-
On Error GoTo Err_cmbreport_Click
-
-
Dim stDocName As String
-
Dim stLinkCriteria As String
-
-
-
-
If Me.grpreports = 6 Then
-
-
DoCmd.Close
-
-
stDocName = "design type"
-
DoCmd.OpenForm stDocName, , , stLinkCriteria
-
-
ElseIf Me.grpreports = 1 Then
-
-
DoCmd.Close
-
-
DoCmd.OpenReport "raw data for a bar", acViewReport
-
-
-
Exit_cmbreport_Click:
-
Exit Sub
-
-
If Err.Number = 2501 Then
-
Resume Next
-
Else
-
Err_cmbreport_Click:
-
MsgBox Err.Description
-
Resume Exit_cmbreport_Click
-
End If
-
-
End If
-
-
-
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?
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: - 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: - DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldLastName = 'Smith'"
Or what about everyone with a date of birth in 1985? - 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
I think some of the lines in your code were out of order. Try this: - Private Sub cmdreport_Click()
-
-
On Error GoTo Err_cmdreport_Click
-
-
Dim stDocName As String
-
Dim stLinkCriteria As String
-
-
If Me.grpreports = 6 Then
-
-
DoCmd.Close
-
stDocName = "design type"
-
DoCmd.OpenForm stDocName, , , stLinkCriteria
-
-
ElseIf Me.grpreports = 1 Then
-
-
DoCmd.Close
-
DoCmd.OpenReport "raw data for a bar", acViewReport
-
-
End If
-
-
Exit_cmdreport_Click:
-
-
Exit Sub
-
-
Err_cmdreport_Click:
-
-
If Err.Number = 2501 Then
-
Resume Next
-
Else
-
MsgBox Err.Description
-
Resume Exit_cmdreport_Click
-
End If
-
-
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
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.
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
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.
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
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.
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: - 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: - DoCmd.OpenReport "Employee Report", acViewPreview, , "tblEmployees.fldLastName = 'Smith'"
Or what about everyone with a date of birth in 1985? - 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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
...
|
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 "...
|
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,...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
| |