Solved

Create a report with subreports in VBA

Posted on 2008-10-03
6
698 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
  • 3
  • 2
6 Comments
 
LVL 84
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 500 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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 84
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 84
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now