Full Version: How To Set Subreport Visible Property by Code
UtterAccess Discussion Forums > Microsoft® Access > Access Reports
imbored
Hello,

This is a continuation from a thread I started about a month ago... but never got a chance to resolve (I had bigger issues at the time...hehe)

In my Issue Tracking database, I would like to print a summary report of the "Issue." On my main Issues Form... depending on the Issue Type selected a different subform is inserted by setting the SourceObject property using a SELECT CASE statement. I would have liked to do the same thing on my Report... but it appears that this ability(Source Object) is not allowed on Reports... any comments?

I am now trying to implement solution 2... setting the Visible Property of my Subreport_Controls. In this case I will place ALL of my Subreports on the same Issue Report and try to control which one is visible. This is the code I am trying to use, any suggestions? What Event should I try? (On the main Report I have tried "On Open", "On Activate" and in the Detail Section of the Report I have tried "On Format" and "On Print")

SELECT CASE [Issue_Type]
Case Me.Issue_Type = "Issue1"
Me.Subreport_Control_Issue1.Report.Visible = True
Me.Subreport_Control_Issue2.Report.Visible = False
Case Me.Issue_Type = "Issue2"
Me.Subreport_Control_Issue2.Report.Visible = True
Me.Subreport_Control_Issue1.Report.Visible = False
END SELECT

In my actual code I will have about 12 of these layered Subreports. Thanks for any suggestions... Im open to anything at this point.

Thanks,
David
jdnichols
In a test using one of my own databases, I changed the source of the subreport using the following code:

CODE


Private Sub Report_Open(Cancel As Integer)



Select Case intVar

    Case 1:

        Me!rpt_Certificate_List_subform.SourceObject = "rpt_Certificate_List_subform1"

    Case 2:

        Me!rpt_Certificate_List_subform.SourceObject = "rpt_Certificate_List_subform2"

End Select



End Sub


If you want to go with the visible/invisible option I got that to work with:

CODE


Private Sub Report_Open(Cancel As Integer)



Select Case intVar

    Case 1:

        Me.rpt_Certificate_List_subform1.Visible = True

        Me.rpt_Certificate_List_subform2.Visible = False

    Case 2:

        Me.rpt_Certificate_List_subform1.Visible = False

       Me.rpt_Certificate_List_subform2.Visible = True

End Select

End Sub


EDITED: Of course I was just using a simple integer variable, not a field from the underlying source. I'll re-test using a field and see what I get.


JD

Edited by: jdnichols on Mon Apr 2 17:34:30 EDT 2007.
jdnichols
Okay, I understand your problem better now. I couldn't get it to change the object source for each record, but it does work if you're only printing one record. That is, if you want to print a report for just the one issue and not for all issues in the table or query, it can work. I tried passing the parameter (Issue_Type in your example) by using OpenArgs, but that doesn't seem to work in AC2003. But when I put the parameter in a global variable, then referenced the var in my Report_Open sub, it worked using the code above. It's 'hacky', but it works.

Hope this is helpful and not just confusing.

Jay
imbored
Hi Jay,

Thanks so much for taking the time to "mock" this up...I really appreciate it. In my case I only need to print data for one record... The "final" detail view of the Issue...all the other reports are just standard list reports.. so your solution should work for me. In your example you tried switching "Subforms".... I tried that...and have since created duplicate subreports for each of the subforms...no help so far. Im going to revisit my code in the morning...comparing with yours. Would it be possible to get a few more detail on using the global var? Ive never done that before. Thanks again.

David
jdnichols
David,

The idea of passing a value in a global variable is to declare a PUBLIC variable in a module (standard module, not a class module). So, if I create a module called Module1, then add the following line:

CODE
[color="blue"]
Public gintIssueType As Integer
  [/color]



I can then reference the variable gintIssueType from anywhere in my project.

So if I set gintIssueType = [Issue_Type] in my form, I can then reference it in the Report_Open sub of my report.


Example form code called when I click the 'Preview Report' button on my form:

CODE
  [color="blue"]
Private Sub cmdPreview_Click()
On Error GoTo Err_cmdPreview_Click

    Dim stDocName As String

    gintIssueType = Me.Issue_Type  'We set the global variable before opening the report

    stDocName = "MainReport"
    ' I'm using the Where clause of the OpenReport method to restrict the report to the current Issue
    DoCmd.OpenReport stDocName, acPreview, , "[Issue_ID] = " & CStr(Me.Issue_ID)

Exit_cmdPreview_Click:
    Exit Sub

Err_cmdPreview_Click:
    MsgBox Err.Description
    Resume Exit_cmdPreview_Click
    
End Sub  [/color]



When my report opens, I use the value in gintIssueType to select the appropriate subreport.


Example Report_Open code:

CODE
  [color="blue"]
Private Sub Report_Open(Cancel As Integer)

Select Case gintIssueType
    Case 1:
        Me.SubRpt1.SourceObject = "SubRpt1"
    Case 2:
        Me.SubRpt1.SourceObject = "SubRpt2"
    Case 3:
        Me.SubRpt1.SourceObject = "SubRpt3"
End Select

End Sub  [/color]

Note: SubRpt1 is used as both the name of the SubReport Control on the main report and as a name of one of the actual subreport objects. Sorry about the confusion.confused.gif

I have attached a working mock-up mdb file. Note: I did this in a hurry, (it's well into happy hour here) so the sub-reports are just useless controls instead of presenting data from a table.

Hope this helps.

Jay o!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.