Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to call code from print button - not working

Status
Not open for further replies.

penndro

Technical User
Jan 9, 2005
108
0
0
US
Hi Can you please offer me some help in getting my code to execute.

I am getting a "Compile Error: Arguement not Optional" error.

I am using a print button to call my print to PDF function:

BUTTON CODE:

Private Sub Comp_Statement_Click()
DoCmd PrintPDF()
End Sub


FUNCTION:

Function PrintPDF(SrcReport As String)

On Error GoTo PrintToPDF_Err

'Call Function from any report with this: "PrintPDF(Report Name)"

Dim FolderPath As String
Dim FileName As String
Dim ShowPdf As Boolean

'Name report to print
SrcReport = "rpt_Compensation_Basic"

'Saves the file to specific directory
FolderPath = " C:\My Documents\" & [FileFolder] & "\"

'" H:\Private\Compensation\2012 Comp Planning\2011-2012 Final Compensation Statements\" & [FileFolder] & "\"

'Formats the file
FileName = [Full Name] & "-" & [Country] & "-" & [Segment] & "-" & [FileFolder]

ShowPdf = False

DoCmd.OutputTo acOutputReport, SrcReport, "PDFFormat(*.pdf)", FolderPath & FileName & ".pdf", ShowPdf, "", 0, acExportQualityPrint

PrintToPDF_Exit:
Exit Function

PrintToPDF_Err:
MsgBox Error$
Resume PrintToPDF_Exit

End Function

Any help would be greatly appreciated.

Thanks
 

You call the procedure with
Code:
DoCmd PrintPDF()
but your procedure is
Code:
Function PrintPDF(SrcReport As String)

First, do not use DoCmd to call a User-Defined Function. DoCmd is for specific Access Functions.

Second, the Function you have is really a Subroutine -- Functions should return a value and yours does not. On top of that, a Function has a variable type corresponding to what information it should return so if you want something returned you should have
Code:
Function PrintPDF(SrcReport As String)as [i]vartype[/i]

Third, the procedure needs to know the name of the report you want to print -- and you need to send it when you call the procedure.
Code:
PrintPDF "[i]MyReportName[/i]"
should do it.

The instructions are included as a comment in the procedure
Code:
'Call Function from any report with this: "PrintPDF(Report Name)"
but that is only correct if you are going to use the returned value in an equation or comparison or to set a variable. Otherwise you do not need the parentheses, as shown in the example above.

The specific error you are getting is due to the missing report name when you call the procedure. But it will still fail if you use DoCmd.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top