Hail to the community!
I've encountered the following problem:
I have a report that I need to export to pdf with a somehow custom procedure. Ideally, using a right click menu command.
In order to test my export procedure I created a test button with the following code that works fine (In case you find any mistakes, please do comment):
Code:
Private Sub Command25_Click()
On Error GoTo Report_klikklak_Error
Dim PdfPath As String
PdfPath = DLookup("MedPrescComment", "tbl_TEMP_MedPrComm", "ID = 2") & "\" 'This value is set in the frm_MedsEdit
Dim fdialog As Office.FileDialog
Set fdialog = Application.FileDialog(msoFileDialogSaveAs)
With fdialog
.AllowMultiSelect = False
.Title = "Choose the export location and filename"
.InitialView = msoFileDialogViewList
.InitialFileName = PdfPath & Pt_FullName & ".pdf" ' Use the name of the patient as filename
If .Show = True Then
'check if the file exists and overwite or exit sub
If Dir(.SelectedItems(1)) <> "" Then
If MsgBox("File Exists, Overwrite?", vbQuestion + vbYesNo, "Overwrite?") = vbYes Then
DoCmd.OutputTo acOutputReport, "rpt_MedsPresc", acFormatPDF, .SelectedItems(1), False, "", , acExportQualityPrint
Else
Exit Sub
End If
Else
DoCmd.OutputTo acOutputReport, "rpt_MedsPresc", acFormatPDF, .SelectedItems(1), False, "", , acExportQualityPrint
End If
End If
End With
Exit Sub
Report_klikklak_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Report_klikklak, line " & Erl & "."
Set fdialog = Nothing
End Sub
Now, I try to create my custom right click menu. Since it has to do only with this report, I've put it in the "On Open" event. And I have created a test public sub - in a module - to test the passing of parameters:
Code:
Private Sub Report_Open(Cancel As Integer)
On Error GoTo Report_Open_Error
' Create the right-click menu
Dim NewMenuBar As Office.CommandBar
Dim PdfPath As String
PdfPath = DLookup("MedPrescComment", "tbl_TEMP_MedPrComm", "ID = 2") & "\" 'This value is set in the frm_MedsEdit
On Error Resume Next
Application.CommandBars("PrescBar").Delete
On Error GoTo 0
Set NewMenuBar = CommandBars.Add("PrescBar", 5, False, True)
'add two native controls
NewMenuBar.Controls.Add 1, 15948, , , True
NewMenuBar.Controls.Add 1, 247, , , True
'add custom export-to-pdf function
With NewMenuBar.Controls.Add(1)
.Caption = "Export to PDF"
.FaceId = 3
.Parameter = PdfPath & Pt_FullName & ".pdf" ' Define the filename's path as parameter to pass to the function ***It cannot find Pt_FullName
.OnAction = "TicToc" ' call the -test- public function "TicToc with the above parameter
End With
On Error GoTo 0
Exit Sub
Report_Open_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Report_Open, line " & Erl & "."
End Sub
The test sub:
Code:
Public Sub TicToc()
MsgBox CommandBars.ActionControl.Parameter
End Sub
It would work ok, except that the menu creation routine cannot find the "Pt_FullName" variable (that is a field of the bound table). I also tried Me.Pt_FullName , nothing. The same variable works ok in the test button. Also, the .Parameter passes correctly the value to the public function (when I try a string or anything else)
Any ideas ?