Solved

Create a report with subreports in VBA

Posted on 2008-10-03
6
714 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

733 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