title: Cycling through all reports in Access
body: <p>I have some code to build a basic report in access but, when I try to loop through all the reports with my variable rpt it skips the loop section, because nothing is assigned to the object. Any Ideas? What I need to get rpt to find the report with the caption qryDummy. Thanks in advance!   :-)</p>
  
  <pre><code> Dim rptReport As Access.Report
   Dim strCaption As String
   Dim rpt As Report     
   CurrentDb.QueryDefs("qryDummy").SQL = strSQL
  
       ' Open dummy query to invoke NewObjectAutoReport command on it
       ' Put the report created to design view to make properties editable
  
       With DoCmd
            .OpenQuery "qryDummy", acViewNormal
            .RunCommand acCmdNewObjectAutoReport
            .Close acQuery, "qryDummy"
            .RunCommand acCmdDesignView
        End With
  
        ' Get reference to just created report 
  
        ' !!!!!!!!!! This is the Section Giving me problems will !!!!!!!!!!!!!!  
        ' !!!!!!!!!! not loop through all the reports. !!!!!!!!!!!!!!!!!!!!!!!!!
  
        For Each rpt In Reports
            If rpt.Caption = "qryDummy" Then Set rptReport = rpt
        Next
  
        With rptReport
  
            ' Create title control
  
            With CreateReportControl(.Name, acLabel, _
                acPageHeader, , ReportTitle, 0, 0)
                .FontBold = True
                .FontSize = 12
                .SizeToFit
            End With
  
            ' Create timestamp on footer
  
            CreateReportControl .Name, acLabel, _
                acPageFooter, , Now(), 0, 0
  
            ' Create page numbering on footer
  
            With CreateReportControl(.Name, acTextBox, _
                acPageFooter, , "='Page ' &amp; [Page] &amp; ' of ' &amp; [Pages]", _
                .Width - 1000, 0)
                .SizeToFit
            End With
  
            ' Detach the report from dummy query
  
            .RecordSource = strSQL
  
            ' Set the report caption to autogenerated unique string
  
            strCaption = GetUniqueReportName
            If strCaption &lt;&gt; "" Then .Caption = strCaption
  
        End With
  
        DoCmd.RunCommand acCmdPrintPreview
  
        Set rptReport = Nothing
  </code></pre>
  
  <h2>EDIT:</h2>
  
  <p>Ok So I guess my problem will use this snippet of code as the report is left open when the VBA runs:</p>
  
  <pre><code>For Each rpt In Reports
            If rpt.Caption = "qryDummy" Then Set rptReport = rpt
        Next
  </code></pre>
  
  <p>The only problem I have is it is not assigning rptReport = rpt I get the error: rpt = nothing, which results in rpt.caption = "Object variable or with block variable not set". So it is like the open report is not being seen? </p>
  
  <p>FYI Solved the Problem need to change rpt.caption to rpt.Name Thanks for the help!</p>