Problem with macro to run a report

I found a macro by Ratslinger from 2018 that looks like it will work (for running a report from a form button) and substituted my relevant info, but I get a “Basic Runtime Error - Variable Not Defined” at the Line “oController = Thisdatabasedocument.currentController”. I have defined all the variables, I think. Any ideas on what is stopping me? The macro is shown below. Thanks.

Rem Macro to run a report (Based on a query)
Sub Run_Report
Dim oDrawPage as Object
Dim oForm as Object
Dim oController as Object
Dim oReportdoc as Object
oDrawPage = ThisComponent.getDrawPage()
oForm = oDrawPage.Forms.getByName(“Customer_Form”)
If oForm.IsNew then
oForm.insertRow()
else
oForm.updateRow()
EndIf
oController = Thisdatabasedocument.currentController
if not oController.isconnected then oController.connect
oReportdoc = Thisdatabasedocument.reportdocuments.getbyname(“Job Schedule Report”).open
End Sub

ThisDatabaseDocument is the embedding document if the code is stored within the database document. I don’t recall, how to access the database document from code that is stored in the global scope (“My Macros”).

The following code tries to get the right object anyway. It will also work with forms and/or reports that are stored in embedded folders. Store the hierarchical name of the report in the button’s “Additional Info”, for instance: Monthly Reports/Sales By Article where “Monthly Reports” is the name of a folder.

Sub Open_Report_Button(e)
REM specify the hierarical name in the button's "Additional info" field
	sName = e.Source.Model.Tag
	doc = getDBDocument(e.Source)
	OpenEmbedded(doc, sName, bReport:=True)
End Sub

Sub Open_Form_Button(e)
REM specify the hierarical name in the button's "Additional info" field
	sName = e.Source.Model.Tag
	doc = getDBDocument(e.Source)
	OpenEmbedded(doc, sName, bReport:=False)
End Sub

Sub OpenEmbedded(odb, sHierachicalName$, bReport As Boolean)
	if bReport then
		container = odb.ReportDocuments
	else	
		container = odb.FormDocuments
	endif
	obj = container.getByHierarchicalName(sHierachicalName)
	obj.open()
End Sub

Function getDBDocument(src):
    oModel = src.getModel()
    ' a button's parent is always a form
    oForm = oModel.getParent()
    oActiveConnection = oForm.ActiveConnection
    oDataSource = oActiveConnection.getParent()
    getDBDocument = oDataSource.DatabaseDocument
End Function

Have you tried omitting this part of the code?
Also no need to declare or define oReportdoc, unless it is a global variable. If it is a global object then

  • oReportdoc = thisDatabaseDocument.reportDocuments.getByName(“Job Schedule Report”)
  • oReportdoc.open()
    Else just open the report with
  • thisDatabaseDocument.reportDocuments.getByName(“Job Schedule Report”).open()
    Here is an abbreviated version of the macro:

Sub Run_Report()
Dim oForm, oReportdoc
oForm = thisComponent.drawPage.forms.getByName(“Customer_Form”)
If oForm.isNew() then
oForm.insertRow()
Else
oForm.updateRow()
Endif
oReportdoc=thisDatabaseDocument.reportDocuments.getByName(“Job Schedule Report”)
oReportdoc.open()
End sub

Sky,
I plugged in your macro instead of what I had.
Now when I run it, I get a Basic syntax error with “thisComponent.drawPage.forms.getByName(” highlighted.
Erik

Replace the quotation marks with straight ones like " and don’t forget to do this also around Job Schedule Report

Sorry… You have to remove the linebreak between “Scedule” and “Report”. It appears that way in the post only because I did not import the code, but merely typed it in the LO ask suggestion format.
The portion of code should actually look like

  • …getByName(“Job Schedule Report”) - No line break