Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create a report with subreports in VBA

Posted on 2008-10-03
6
Medium Priority
?
736 Views
Last Modified: 2013-11-05
Hi Experts

Can you create a report and then add X number of subreports?
0
Comment
Question by:DCRAPACCESS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 85
ID: 22633038
In VBA? Sure, using CreateReport ... but it's definitely not a recommended method. Doing so will bloat your database, and will almost certainly cause corruption, instability etc etc.

You can always change the Recordsource and such of your reports on the fly ... this is the better method of handling "dynamic" reporting requirements.
0
 

Author Comment

by:DCRAPACCESS
ID: 22633086
Can you give a code example of how to do this?
0
 
LVL 61

Accepted Solution

by:
mbizup earned 2000 total points
ID: 22633140
> Can you create a report and then add X number of subreports?

Yes.

The commands you need are:

CreateReport
CreateReportControl

Read up on these in VBA's help, and also on the CreateControl method.  

This can get very complicated very fast (beyond the scope of an EE question), depending on what you need to do with the subreports' properties.

In its simplest form, the VBA would look like this:

Function CreateReports()
    Dim rpt As Report
    Dim ctl As Control
    Dim x As Integer
   
    ' Create the parent report
    Set rpt = CreateReport
    ' You can set report  properties here:
    ' rpt.Caption = ...
    ' rpt.recordsource = ...
    ' etc
   
    ' Add the subreport controls
    For x = 0 To 3
        Set ctl = CreateReportControl(rpt.Name, acSubform, acDetail, rpt.Name, , 15, x * 300, 300, 300)
        ' set subreport properties here
        ctl.SourceObject = "rpt" & x + 1  ' sets source report to "rptN"
        ' ctl.recordsource = ...
        ' etc
    Next
   
    'this will restore the report, which is currently minimized in design view.
    DoCmd.Restore
   
    Set ctl = Nothing
    Set rpt = Nothing
   
End Function
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 61

Expert Comment

by:mbizup
ID: 22633172
<and will almost certainly cause corruption, instability etc etc. >

Interesting --
I knew this was an unconventional, non-optimal way of doing things but I wasn't aware of the corruption issues.
0
 
LVL 85
ID: 22633258
I've never used the methods so cannot comment on this personally, but the newsgroups have ample evidence of this occurring, and many an EE Expert and Microsoft MVP have advised against this practice. You would probably be okay with small objects - perhaps a small form with no code, or a simple report with default values and very few controls - but it will significantly bloat the database. In certain cases - for example, when trying to add a module to a form/report you're creating on the fly - it's caused catastrophic VB container corruption.

And of course you cannot do this type of thing if you deploy an .mde file ... which is what you should be doing if you are deploying this to endusers.

0
 
LVL 85
ID: 22633279
You can change the Recordsource of your report in the report's Open event. Many devs use a hidden form for this - or your "main menu" form, which is always open. To do this:

1) Add a textbox to your form. Make it hidden if you wish
2) BEFORE you open your report, write the Recordsource you want to use to that hidden textbox
3) When the report opens, set the Recordsource as such:

Sub Report_Open()
  Me.Recordsource = Forms("YourFormName").YourTextboxName
End Sub

YOu can also control Sorting, Filtering, Groupings etc in much the same fashion. I have a Class Module that I use for this, but a hidden form would work as well.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question