?
Solved

Create a report with subreports in VBA

Posted on 2008-10-03
6
Medium Priority
?
729 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 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