Solved

Create a report with subreports in VBA

Posted on 2008-10-03
6
692 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

706 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

19 Experts available now in Live!

Get 1:1 Help Now