Results 1 to 4 of 4
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Need help baking reportname and filename into export VBA

    I have the below vba to export a report from access. It works great but I was looking for help baking in the reportname and filename lines. Whenever I copy this code to use in a different report, these two fields need to be changed. I would love if the reportname just automatically uses the report's name that the VBA is in (if that makes sense). Also, it'd be great if the filename could be based off a label field in the report (the field is titled lblReportName).



    Any suggestions?

    Code:
    Private Sub btnExportReport_Click()On Error GoTo ErrHandler
        Dim reportName As String
        Dim fd As Object
        Dim filename As String
       
        reportName = "rptAnnualComparsionQUARTER"
    
    
        Set fd = Application.FileDialog(2)
        filename = "Annual Comparsion by Quarter" & " " & Format(Date, "mm.dd.yyyy") & ".pdf"
        
        With fd
            .Title = "Save to PDF"
            .InitialFileName = "\Documents\" & filename
            If .Show = -1 Then
                filename = fd.SelectedItems(1)
                If InStr(filename, ".") = 0 Then
                    filename = filename & ".pdf"
                ElseIf Right(filename, 4) <> ".pdf" Then
                    k = InStrRev(filename, ".") - 1
                    filename = Left(filename, k)
                    filename = filename & ".pdf"
                End If
            
            DoCmd.OutputTo acOutputReport, reportName, acFormatPDF, filename
    
    
    
    
            MsgBox "Report saved to " & filename
            
            End If
        
        End With
        
    ExitHandler:
        Set fd = Nothing
        Exit Sub
        
    ErrHandler:
        If Err.Number = 2501 Then
            MsgBox "Access cannot save this PDF because a PDF with the same name is currently open. Exit out of the PDF and then export again."
        Else
            MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Export Error"
        End If
        
        Resume ExitHandler
    End Sub

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Maybe try:
    Code:
    reportName = "rptAnnualComparsionQUARTER"
    reportName=Me.Name 'assumes the button is on the report itself not on a form
    fileName-Me.lblReportName.Caption
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Join Date
    Feb 2022
    Posts
    4
    To capture the name of the report that is active and opened, dim a variable to capture the name and then use code to populate that variable with the opened active report name. Example:

    Dim strReportName as String
    strReportName = reports(currentreport).name

    I hope that helps. Good Luck!

  4. #4
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    This worked!! Thank yoU!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 15
    Last Post: 12-31-2015, 02:26 PM
  2. Export table to txt file with a variable filename
    By macollins7 in forum Import/Export Data
    Replies: 6
    Last Post: 07-12-2012, 09:44 AM
  3. Variuse FileName When Export
    By shay in forum Import/Export Data
    Replies: 1
    Last Post: 12-01-2010, 11:36 AM
  4. Prompt for filename for import/export??
    By stephenaa5 in forum Import/Export Data
    Replies: 23
    Last Post: 10-23-2009, 03:43 PM
  5. Replies: 0
    Last Post: 06-11-2009, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums