Results 1 to 5 of 5
  1. #1
    catluvr is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Kansas
    Posts
    87

    Question Passing a string from one subroutine to another

    Hello All ...

    I'm pretty sure this is super simple and I'm overlooking something pretty obvious ...

    I did search the forum, because I'm also pretty sure this is a common questions, but I couldn't find what I was looking for

    I have a bunch of buttons that, when clicked, send a report to PDF. These are all based on saved exports, which means that i have to update the path in the saved export. I'd rather not do that.

    This is an annual database, so each year, the database is copied into a new folder that is named for the year. That's why the path changes each year.

    I have a sub-routine that will do exactly what I need ... but I then have to copy the sub-routine to each command button and change the file name and report name.

    I was hoping I could have a sub with those mechanics and then for the button, i could just define the file name and report name and then call the sub with the mechanics and have it run.

    But ... it doesn't work ... why not?

    The code behind the button ...



    Code:
    Private Sub cmdTrpsNum_Click()' Saves the Troops by Number report as a PDF.
    Dim FileName As String
    Dim ReportName As String
    
    
    FileName = "Troops by Number"
    ReportName = "FullTroop Report"
    
    
    Call Export2PDF
    
    
    End Sub
    My "mechanics" code ...

    Code:
    Sub Export2PDF()
    
    Dim strPath As String
    Dim strDate As String
    Dim FullFileName As String
    Dim FileName As String
    Dim ReportName As String
    
    
    
    
    'find the directory to use based on the location of the database file
    strPath = CurrentProject.Path & "\"
    
    
    'Build file name for the report. It will be in the format: path\filename
    FullFileName = strPath & FileName
    
    
        DoCmd.OpenReport ReportName, acViewPreview
        'Send the report to PDF and close the PDF file
        DoCmd.OutputTo acOutputReport, , acFormatPDF, FullFileName & ".pdf", False
        'Close the report and don't save it
        DoCmd.Close acReport, ReportName, acSaveNo
       
    End Sub
    How do i send the file name and report name from the first to the second?

    My hope is to then have several (maybe six) of the smaller code ... each button would have a different file name and report name .... and just the one larger code ...

    Thanks!

    Susie
    Kansas

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    FileName = "Troops by Number"
    ReportName = "FullTroop Report"

    Export2PDF filename, reportName


    Code:
    Sub Export2PDF(pvFile, pvRpt)
    
    FullFileName = strPath & pvFile & ".pdf"
    
        DoCmd.OpenReport pvRpt, acViewPreview
        DoCmd.OutputTo acOutputReport, pvRpt, acFormatPDF, FullFileName , False
    
    end sub

  3. #3
    catluvr is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Kansas
    Posts
    87
    I'm sorry, I don't quite understand.

    So the code behind the button should be:

    Code:
    Private Sub cmdTrpsNum_Click()' Saves the Troops by Number report as a PDF.
    Dim FileName As String
    Dim ReportName As String
    
    
    FileName = "Troops by Number"
    ReportName = "FullTroop Report"
    
    
    Call Export2PDF FileName, ReportName
    
    
    End Sub
    And the called code should say ...

    Code:
    Sub Export2PDF(FileName, ReportName)
    
    
    Dim strPath As String
    Dim strDate As String
    Dim FullFileName As String
    Dim FileName As String
    Dim ReportName As String
    
    
    
    
    'find the directory to use based on the location of the database file
    strPath = CurrentProject.Path & "\"
    
    
    'Build file name for the report. It will be in the format: path\filename
    FullFileName = strPath & FileName
    
    
    
    
        DoCmd.OpenReport ReportName, acViewPreview
        'Send the report to PDF and close the PDF file
        DoCmd.OutputTo acOutputReport, , acFormatPDF, FullFileName & ".pdf", False
        'Close the report and don't save it
        DoCmd.Close acReport, ReportName, acSaveNo
        
    
    
    End Sub
    Is that correct? (changes in green)

    Susie
    Kansas

  4. #4
    davegri's Avatar
    davegri is online now Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,411
    The call should be:
    Code:
    Private Sub cmdTrpsNum_Click()    'Saves the Troops by Number report as a .PDF
    Dim FileName As String
    Dim ReportName As String
    FileName = "Troops by Number"
    ReportName = "FullTroop Report"
    Call Export2PDF(FileName, ReportName)
    ' or
    'Call Export2PDF("Troops by Number","FullTroop Report")
    End Sub
    and the sub:


    Code:
    Sub Export2PDF(FileName as string, ReportName as string)
    Dim strPath As String
    Dim FullFileName As String
    
    
    'find the directory to use based on the location of the database file
    strPath = CurrentProject.Path & "\"
    
    
    'Build file name for the report. It will be in the format: path\filename
    FullFileName = strPath & FileName
    
    
        DoCmd.OpenReport ReportName, acViewPreview
        'Send the report to PDF and close the PDF file
        DoCmd.OutputTo acOutputReport, , acFormatPDF, FullFileName & ".pdf", False
        'Close the report and don't save it
        DoCmd.Close acReport, ReportName, acSaveNo
    End Sub
    BTW, the green was virtually invisible on my monitor.
    Last edited by davegri; 08-01-2018 at 04:09 PM. Reason: alternate code

  5. #5
    catluvr is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2018
    Location
    Kansas
    Posts
    87
    Perfect ... exactly what I wanted! This will save me typos ... which is what usually happens when I copy and paste the same sub-routine and change the variables.

    Thank you!

    Susie
    Kansas

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

Similar Threads

  1. Query not running right in subroutine
    By Historypaul in forum Programming
    Replies: 8
    Last Post: 01-29-2013, 06:42 AM
  2. Passing Multi Select string to Query criteria
    By oleBucky in forum Queries
    Replies: 4
    Last Post: 05-15-2011, 02:11 PM
  3. Calling Access VBA subroutine from an Excel VBA subroutine
    By richard_yolland in forum Programming
    Replies: 0
    Last Post: 02-16-2011, 11:30 AM
  4. Referencing a Subroutine
    By Lockrin in forum Programming
    Replies: 1
    Last Post: 02-26-2010, 10:09 AM
  5. Passing a form name to a subroutine
    By trb5016 in forum Programming
    Replies: 0
    Last Post: 02-01-2010, 12:03 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