Question

Passing Parameters from Report to SubReport

Asked by: DaveSavittiere

Hello Experts!

I have been having a terrible time trying to figure this out, so any help is EXTREMELY appreciated!

Here's the problem:

I am designing an Access Project with Access 2000 on an XP sp1 workstation connected to an SQL Server 2000 sp3.  In my project I have several reports, all of which get their data through stored procedures.  The one report in question has 4 subreports that each have a different stored procedure.  Each of those SP's require a parameter, but the parameter is the same for all...

EXAMPLE:

rptMAIN.recordsource = Report_QuarterBudget '1st Quarter'
rptSub1.recordsource = Report_QLocationBudget '1st Quarter'
rptSub2.recordsource = Report_QLocationWedge '1st Quarter'
rptSub3.recordsource = Report_QLocationBudgetSummary '1st Quarter'
rptSub4.recordsource = Report_QLocationWedgeSummary '1st Quarter'

I have a form, frmMenuReports, that has a list box populated with the names of all available reports.  When the user chooses the report s/he wants to run, s/he clicks the item in the list box which then opens an option to run that report as a total of all quarters, or by specific quarter.  If the user chooses '1st Quarter', I need to pass that string value to rptMain and all it's subs as the parameter for each SP.  When I run the report from the database window, without using my frmMenuReports, a dialog appears asking me for the parameter.  I enter '1st Quarter' and it runs just fine, except that it asks me for the paramater for each SP that is being executed and then a couple more.  Actually, it asks me for the parameter 9 times before the report actually prints, but it ends up printing correctly.  I really need to be able to drive this report from a menu without having the user type out '1st Quarter' and I cannot figure out how to pass the ONE PARAMETER to the five SP's....  

Please let me know if I need to clarify any further, and thanks in advance!!

Dave Savittiere

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-05-24 at 13:30:14ID21435401
Tags

subreport

,

access

Topic

Microsoft Access Database

Participating Experts
3
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. How to pass parameter to subreport
    I am using the crystal report 8.0. and i am calling this report from VB6. in VB form i am using crystal report component. i have to pass parameter to Subreport and main report i am passing parameter to main report like this CRreport.ParameterFields(0) = "Rep_Pa...
  2. Subreports
    We are trying to use a subreport, but now in Crystal Reports 10 we cannot use the Load() method or the SetParameterValue() method. How am I supposed to get the DataSource and Parameters set for the subreport if I can't even load it! TIA Mike
  3. Passing Formula to Subreport
    Hi, I have a problem. I have a main report that has a Paramter Field "Year" I have also two subreports which does a subreport link to the Parameter Field. These subreports uses SQL Command and has parameter in the SQL query as "Reporting Year"...
  4. Parameters Passed To SubReports
    CR 8.5. This question has been asked hundreds of times according to my search. However, the answers previously provided did not help. I have a Report with a SubReport. I have a web-based application that is accessing these reports. There are three parameters passed...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: kirenievsPosted on 2005-05-24 at 14:20:42ID: 14072642

I assume the parameter is in the queries. Maybe you could change the parameter in all queries to

   Forms!frmMenuReports!TheListBox

 

by: ArjiPosted on 2005-05-24 at 17:28:47ID: 14073831

Have you considerd linking the main report to your subreports? Especially since they all require the same parameter.  That way all you have to do is pass the parameter to the main report and all the subreports will follow.  You do this with the Master Link Fields and Child Link Fields of each subreport's control properties.  Use your parameter field or the primary key for each subreport so that the Master Link Field = the Chld Link Field on each subreport. Continue to use the same store procs you are using.

 

by: DaveSavittierePosted on 2005-05-25 at 14:22:52ID: 14081630

I am not sure how to link the parameters by the Master/Child link fields and I am pretty lost when you say to link the main report to the subs...  
The thing is, all five reports are really seperate reports, but need to be displayed on the one page. I guess instead of using stored procedures that require a parameter ( for the quarters), I could hard code it into the SPs and just use five reports (A total, 1st Q, 2nd Q, 3rd Q, and a 4th Q) and depending on which Quarter the user wants output determines which report runs although I don't think that is neccessary since the format is the exact same for each, just the data is different.  Could this be a limitation of ACCESS 2000?

Another Question...  If I declare a public string variable, behind my report, and load it with the parameter needed to run the SP, why does ACCESS error and tell me that the SP does not exist or is mispelled.  for instance:

Option Compare Database
Option Explicit

     Dim Timing As String
     Dim str As String

Private Sub Report_Open(Cancel As Integer)

Timing = "'1st Quarter'" '<--- I could load this into an invisible text box if it would actually find the SP
                                   ' so that Timing = me.txtInvisbleParameter
                         
str = "Report_QuarterBudget " & Timing

Me.RecordSource = str

DoCmd.Maximize

End Sub

OR THIS:

Option Compare Database
Option Explicit

Dim Timing As String
Dim str As String

Private Sub Report_Open(Cancel As Integer)

Timing = "1st Quarter"

str = "Report_QuarterBudget '" & Timing & "'"

Me.RecordSource = str

DoCmd.Maximize

End Sub

In Query Analyzer, I run the SP like this:

Report_QuarterBudget '1st Quarter'

and it runs perfectly, why does Access have such a problem with that?


Thanks again for your help!

 

by: ArjiPosted on 2005-05-25 at 15:37:58ID: 14081993

>The thing is, all five reports are really seperate reports, but need to be displayed on the one page<

That's one of the great things about subreports.  The best way to link subreports to main reports is to use the 'subreport wizard'.  You don't even need to design the subreports intially, just fix them up when the wizard is done.  Create/open a 'Main' report in design mode.  Now from the design toolbox, select the subform/subreport tool and then drag a rectangle whereever you want to place your first subreport on the main report.  When you let go of the rectangle, the wizard will start up and guide you through the process.  Select the 'Use Existing Tables and Queries' option.  In the next step, select the SP you want to use as the recordsource for the subfrom.  In the next step, select the 'Define My Own' option.  Now in the pulldown lists for the fields, select a field(Report_QuarterBudget) from the main form and a matching field(Report_QuarterBudget) from the subform.  This will link the two forms by way of the fields you just selected.  In the next step, name the subform something that makes sense like 'frm1st_Qtr_subform' and then finish the wizard.  You'll now see the subform displayed within your main form and they are now linked.  Whenever you open the main report with a parameter, the subreport will display the information related to that parameter:

DoCmd.OpenReport "YourMainReportName", acViewPreview, , "Report_QuarterBudget '" & Timing & "'"

BUT.....You could actually place the value of 'Timing' in a text box and pass the text box value instead of a global variable:

DoCmd.OpenReport "YourMainReportName", acViewPreview, , "Report_QuarterBudget '" & me.txtInvisbleParameter & "'"

Use a command button to execute the OpenReport.  Passing a value to a report is by far the best way to do this.  That way you don't need the On Open code to define the recordsource.

I would highly recommend you learn about subforms and subreports.  They are a very valuable tool when developing database applications.  The more you learn about them the more ways you will see to use them.

>Another Question...  If I declare a public string variable, behind my report, and load it with the parameter needed to run the SP, why does ACCESS error and tell me that the SP does not exist or is mispelled. <

I think that is an erroneous error. :-)  Sometimes errors are not very clear.  If you want to declare a public variable, you should do that in a Module.  However you need to use this format:

Global Timing as String

This will make the variable available anywhere in the app.  I don't think you need a Global variable in this case though.  Let me know if you need some more help.

 

by: Sard0nicpanPosted on 2005-05-25 at 15:58:34ID: 14082086

Hey Dave,

Perhaps its a matter of Access 2000 Standard edition vis a vis Professional?

 have an Access 2000(standard edition) project where I want to set my Report recordsource dynamically in VBA.  I coded according to the following link:

http://support.microsoft.com/?kbid=300693

So you'd think it would work, right? No.  My project cannot find the stored procedure.  If I take away the "Exec" and the parameter part of the statement it works (and then prompts me for the parameter).  See code:

[code]
Private Sub Report_Open(Cancel As Integer)
    Dim strRecordSource As String
    strRecordSource = "Exec spA1 'td962'"
    Me.RecordSource = strRecordSource
End Sub
[/code]

Ok so I tried to see if I could set up a Northwind.ADP like in the link and got the "cannot find" statement also.  This is even though I just cut and paste the same code from the link above.

[code]
Private Sub Report_Open(Cancel As Integer)
    Dim strRecordSource As String
    strRecordSource = "Exec [Sales By Year] '1/1/97','12/31/98'"
    Me.RecordSource = strRecordSource
End Sub
[/code]

So the only thing I can figure is that perhaps this code works with professional 2000 and not with standard.
Any ideas?  What do ya think?

thanks in advance,
SP



 

by: ArjiPosted on 2005-05-25 at 16:33:48ID: 14082209

Sard0nicpan,
There is no difference between Accesss 2k and 'professional'. Professional is an Office designation.  Office professional includes Access and I believe Publisher.  Standard doesn't.  Passing a value with the OpenReport method is much more versatile even if you want to use individual reports because you can use the same report for all your quarterly reports.  Just pass the value to the report.  Your method will work but it will cause you to hardcode the values in the On Open event of each report.  As long as you have working SPs, passing the value will work just fine.  There is also a parameter for OpenReport called OpenArgs.  You can use OpenArgs to send a new Title to each report so you can use one report and change the title depending on what you are printing. There are a lot more features of OpenReport.  It is the best method for opening reports.

 

by: DaveSavittierePosted on 2005-05-26 at 08:29:50ID: 14086918

Arji,

I realy appreciate your help, but I can't seem to follow your advice, and here's why....

This is an Access Project (.adp) and I am connected to an SQL Server, so when I tried redesigning the report using the subreport wizard, the only options I get is to use 'Existing Tables or Views', or 'Existing Forms or Reports'.  The problem is that I cannot create a view that requires a parameter to return a specific recordset like a query.  The only option I see to use is a Stored Procedure...  Am I wrong?

For this question, I will use 2 stored procedures (and give the code) so maybe I can clarify the problem a little better than I have in the past.

Here it is:

01.  The main report is a Budget summary of projects my company will do this year.
02.  There are 5 columns that make up the summary.  They are as follows:

              PROJECTTYPE (a category name given to each project)
              BUDGET          (in the db, this denotes a project that is definitely going to be done this year)
              WEDGE           (in the db, this denotes a project that might be done this year)
              RISK               (because a wedge item might NOT be done, we RISK the total estimate for the Wedge item by 50%, so RISK = WEDGE/2)
              TOTAL            (this is a total of BUDGET + RISK)

03.  There are 2 subreports on the main report that are breakdowns of the entire summary
04.  The first subreport is a breakdown of the WEDGE items sorted by PROJECTTYPE, then by LOCATION
05.  The first subreport contains its own sub report that summarizes the Wedge items only by LOCATION and does not take into account the PROJECTTYPE
06.  The secon subreport, on the main form, is a breakdown of all the Budget items by PROJECTTYPE, then LOCATION and also contains its own subreport...

07.  The main reports name is TOTALSBYPROJECTTYPEQ.
08   The Q at the end is my way of saying this is going to be the report that we can use to filter out the totals by QUARTER
09.  The Stored Procedure that I use for it's record set is REPORT_QUARTERBUDGET and the code is as follows:

     ALTER  PROC Report_QuarterBudget (@Timing varchar(25))
     AS

     SELECT A.ProjectType, A.Budget,
         Wedge = CASE WHEN A.wedge IS NULL THEN 0 ELSE A.wedge END,
         Risk = CASE WHEN A.risk IS NULL THEN 0 ELSE A.risk END,
        Total = CASE WHEN SUM(A.Budget + A.Risk) IS NULL THEN SUM(A.Budget + 0) ELSE SUM(A.Budget + A.Risk) END
     FROM

     (

     SELECT B.ProjectType, B.[Net] AS Budget, W.[Net] AS Wedge, W.Risk
     FROM

      (
      SELECT ProjectType, SUM(CurrentNet) AS [Net], BudgetType
      FROM ProjectView
      WHERE BudgetType = 'Budget' and Timing = @Timing
      GROUP BY ProjectType, BudgetType
      ) B

     LEFT OUTER JOIN

      (
      SELECT ProjectType, SUM(CurrentNet) AS [Net], SUM((CurrentNet)/2) AS [Risk], BudgetType
      FROM ProjectView
      WHERE BudgetType = 'Wedge' and Timing = @Timing
      GROUP BY ProjectType, BudgetType
      ) W

     ON  B.ProjectType = W.ProjectType

     GROUP BY B.ProjectType, B.[Net], W.Risk, W.[Net]
     ) A

     GROUP BY A.ProjectType, A.Budget, A.Wedge, A.Risk

10.  As you can see, the SP requires the @Timing variable which could be '1st Quarter', '2nd Quarter', '3rd Quarter' or '4th Quarter'
11.  The stored procedure for the first subreport also requires the @Timing variable, as it is a breakdown of the summary as a whole.  the code is as follows:

     ALTER PROC Report_QLocationWedge (@Timing varchar(25))
     AS

     SELECT Location, CurrentNet AS Unrisked, SUM(CurrentNet / 2) AS RISKed
     FROM ProjectView
     GROUP BY ProjectID, Project, Location, CurrentNet, BudgetType, Timing
     HAVING (BudgetType = 'Wedge') and Timing = @Timing

12.  As you can see, it is a small portion of the whole, but adds in the LOCATION part

There really isn't a field I can link together, that I can see, so if I could pass the @Timing Variable from the main report to the sub report without the user typing it in several times, that would be great.  Right now I have a form (frmMenuReports) that has a list box (lstReports) that looks to a table (Reports) and lists the Report names as I want them displayed.  When the user opens frmMenuReports, They can click the option from lstReports that displays "Budget Summary by Quarter".  Once clicked, it opens an option frame (fmeQuarterOption) where the user will choose which Quarter they want the report to print.  When the user chooses an option, it sets the string variable (TIMING) to '1st Quarter', '2nd Quarter', '3rd Quarter' or '4th Quarter'.  On the bottom of frmMenuReports, there is a command button (cmdRunReport) that opens the report chosen, and if it is this particualr report, it opens and passes the variable TIMING as the parameter needed for the stored procedure, Report_QuarterBudget, as well as the parameter for the other stored procedures for all the subforms.  This way the user never has to acutally type anything out and everything is dynamic....  

Thanks Again!

 

by: ArjiPosted on 2005-05-26 at 09:35:49ID: 14087662

I would take this in sections to build your report.  

1. Backup up your objects first
2. Remove the parameter @Timing from your SP (the easiest way is open the SP in the query designer)
3. Remove the parameter @Timing from your SP for the 1st subreport
4. Place link the 1st subreport to the main reports using the PROJECTTYPE fields of both entities.  This should represent a one to many relationship between your summary and the breakdown of WEDGE items(1 summary to many WEDGE items.  It appears that you could even link them using the Timing field but I don't see a Timing field anywhere...just the parameter @Timing.  You did state that the 1st subform also requires the @Timing param.
5. Open the report with:
              DoCmd.OpenReport "TOTALSBYPROJECTTYPEQ", acViewPreview, , "Timing'" & me.txtInvisbleParameter & "'"

Before you do however, you should have a field named 'Timing' being returned by your SP.  I don't see the field but I do see the statement Timing = @Timing.  I see the same thing in the SP for your 1st subreport.  What field do you have the @Timing parameter assigned to in your SP?  I just don't see it in the SP.  Also I don't see the field PROJECTTYPE in the second SP for the 1st subform.  You stated you are sorting by this field but I don't see it anywhere.

Let me know what you have.

 

by: DaveSavittierePosted on 2005-05-26 at 09:55:24ID: 14087854

Arji,

You are correct, the second subform's Stored Procedure should be:

     ALTER PROC Report_QLocationWedge (@Timing varchar(25))
     AS

     SELECT ProjectType, Location, CurrentNet AS Unrisked, SUM(CurrentNet / 2) AS RISKed
     FROM ProjectView
     GROUP BY ProjectType, ProjectID, Project, Location, CurrentNet, BudgetType, Timing
     HAVING (BudgetType = 'Wedge') and Timing = @Timing


I was in the middle of testing siome consolidation of stored procedures and pasted the wrong code...  

Ok, I think I see where you're coming from.  There is a field in my table called TIMING...  But, if I remove the @Timing variable from the Stored Procedures, how will I return the recordset of all items for only '1st Quarter', '2nd Quarter', '3rd Quarter' or '4th Quarter'?

That's what I'm doing here, right?:    DoCmd.OpenReport "TOTALSBYPROJECTTYPEQ", acViewPreview, , "Timing'" & me.txtInvisbleParameter & "'"
     (just as an aside, did you mean   "Timing = '" & me.txtInvisibleParameter & "'" ?)

I'll give that a try...

Dave

 

by: ArjiPosted on 2005-05-26 at 10:08:40ID: 14087968

>(just as an aside, did you mean   "Timing = '" & me.txtInvisibleParameter & "'" ?)<

Yeah, sorry... and yes, you would pass the value to the report that based on the SP.  It works like a query in an .MDB.  You would put a parameter in a query for a report because that will cause the query to prompt the user(unless that what you want).  In most cases, you would want the user to enter a value on the form that's calling the report.  Like maybe a combobox that lists the possible values that could be passed to the report.  Your combobox would be based on a 'Value List' that contains '1st Quarter', '2nd Quarter', '3rd Quarter' or '4th Quarter' respectively.  You would then pass that value to the report:

DoCmd.OpenReport "TOTALSBYPROJECTTYPEQ", acViewPreview, , "Timing='" & me.ComboxBox & "'"

You could create a small table with your values in it and then use the table as the rowsource of the combobox but a Value List is the quickest way.


20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...