report listed in combo box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stoic
    New Member
    • Jun 2012
    • 68

    #1

    report listed in combo box

    Hi,
    I have an Access 2007 database with a form called 'Report Menu'. I would like to create a combo box that shows all the reports in the report as a dropdown. I would also like to include on that form, a button that, will preview the selected report from the combo box, when clicked.
    I will appreciate if anyone can help me out on this.
    Thanks.
    Oscar
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    The following Code, placed in the Open() Event of your Form, will populate a Combo Box named cboReports with all the Reports in your Database. The rest is relatively easy to accomplish.
    Code:
    Private Sub Form_Open(Cancel As Integer)
    Dim obj As AccessObject
    Dim objApp As Object
    Dim strRpts As String
    
    Me![cboReports].RowSourceType = "Value List"
    
    Set dbs = Application.CurrentProject
    
    If dbs.AllReports.Count = o Then Exit Sub
    
    'Search for ALL AccessObject objects in AllReports collection.
    For Each obj In dbs.AllReports
      'Me![cboReports].AddItem obj.Name   'Alternative approach
       strRpts = strRpts & obj.Name & ";"
    Next obj
    
    'Populate Combo Box with List of all Reports in the Database. 
    'Remove Trailing ";", (Not  necessary if using AddItem)
     Me![cboReports].RowSource = Left$(strRpts, Len(strRpts) - 1)
    End Sub

    Comment

    • Stoic
      New Member
      • Jun 2012
      • 68

      #3
      Originally posted by ADezii
      The following Code, placed in the Open() Event of your Form, will populate a Combo Box named cboReports with all the Reports in your Database. The rest is relatively easy to accomplish.
      Code:
      Private Sub Form_Open(Cancel As Integer)
      Dim obj As AccessObject
      Dim objApp As Object
      Dim strRpts As String
      
      Me![cboReports].RowSourceType = "Value List"
      
      Set dbs = Application.CurrentProject
      
      If dbs.AllReports.Count = o Then Exit Sub
      
      'Search for ALL AccessObject objects in AllReports collection.
      For Each obj In dbs.AllReports
        'Me![cboReports].AddItem obj.Name   'Alternative approach
         strRpts = strRpts & obj.Name & ";"
      Next obj
      
      'Populate Combo Box with List of all Reports in the Database. 
      'Remove Trailing ";", (Not  necessary if using AddItem)
       Me![cboReports].RowSource = Left$(strRpts, Len(strRpts) - 1)
      End Sub
      Hi ADezii,
      Thanks again. This is working perfectly well, but I still need your help on the button the previews the report.
      Thanks

      Comment

      • Mihail
        Contributor
        • Apr 2011
        • 759

        #4
        Under _Click event place one of the following line:

        Code:
        'To preview:
        DoCmd.OpenReport "ReportName", acViewReport
        
        'To print:
        DoCmd.OpenReport ("ReportName")
        
        'To save as .rtf :
        DoCmd.OutputTo acOutputReport, "ReportName", "RichTextFormat(*.rtf)", , True

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Code:
          If IsNull(Me![cboReports]) Then Exit Sub
          
          'To Open the selected report in Preview Mode
          DoCmd.OpenReport Me![cboReports], acViewPreview

          Comment

          • Stoic
            New Member
            • Jun 2012
            • 68

            #6
            Originally posted by ADezii
            Code:
            If IsNull(Me![cboReports]) Then Exit Sub
            
            'To Open the selected report in Preview Mode
            DoCmd.OpenReport Me![cboReports], acViewPreview
            Thanks again ADezii, I am very grateful.

            Comment

            • Stoic
              New Member
              • Jun 2012
              • 68

              #7
              Originally posted by Mihail
              Under _Click event place one of the following line:

              Code:
              'To preview:
              DoCmd.OpenReport "ReportName", acViewReport
              
              'To print:
              DoCmd.OpenReport ("ReportName")
              
              'To save as .rtf :
              DoCmd.OutputTo acOutputReport, "ReportName", "RichTextFormat(*.rtf)", , True
              Thanks Mihail, but this is not what I wanted.
              Cheers!

              Comment

              • Stoic
                New Member
                • Jun 2012
                • 68

                #8
                Originally posted by ADezii
                Code:
                If IsNull(Me![cboReports]) Then Exit Sub
                
                'To Open the selected report in Preview Mode
                DoCmd.OpenReport Me![cboReports], acViewPreview
                Hi ADezii,
                I just want to thank you for your brilliant answer to my question on populating my reports to a combo box on my form. It works very well.

                I also have a problem with my dlookup. I have developed an entry form with a combo box it containing a list of school codes. I would like to populate other details from the school table to the fields on the form for data entry purpose. This is what I have so far but is not working:

                Me.SName = DLookup("[SchoolName]", "tblSchools ", "[cboSchoolCode]='" & Me.[cboSchoolCode] & "'")

                Me.txtCounty = DLookup("[County]", "tblSchools ", "[cboSchoolCode]='" & Me.[cboSchoolCode] & "'")

                But this is not just working.
                Thanks for your assistance.
                Oscar

                Comment

                • ADezii
                  Recognized Expert Expert
                  • Apr 2006
                  • 8834

                  #9
                  Assuming School Codes are String Values, your problem lies in the WHERE Clause of DLookup(). You need to reference the 'Name' of the [School Code] Field in tblSchools, and not the Name of the Combo Box. Some examples 'may' be:

                  Code:
                  Me.SName = DLookup("[SchoolName]", "tblSchools", "[SchoolCode]='" & Me.[cboSchoolCode] & "'")
                  
                  Me.SName = DLookup("[SchoolName]", "tblSchools", "[School Code]='" & Me.[cboSchoolCode] & "'")

                  Comment

                  Working...