Problem with macro to run a report

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

Sky,
My version doesn’t have a line break between “Schedule” and “Report”.
The run time error is at the line “thisComponent.drawPage.forms.getByName(”.
Any other possibilities?
Erik

I implemented Wanderers suggestion - to replace the diagonal quotes with vertical quotes and am still getting an error at the same line, but it now the hole line highlighted as a problem. The highlighted line is " oForm = thisComponent.drawPage.forms.getByName(“Customer_Form”)"
Thanks, Erik

Sky,
Actually now the error at that line says: “Property or method not found: drawPage.”
with “oForm = thisComponent.drawPage.forms.getByName(“Customer_Form”)” highlighted.
Erik

Did you try my macro? You don’t need to understand the code. You just need to know how to use it.

Wanderer,
Good advice. The quotes have gotten me before when I copy paste code.
Thanks, Erik

Something is wrong with the name supplied for the actual form control. The form control name is indicated in the form navigator, not in the odb Forms list. To avoid this confusion, use the getByIndex() method instead of the getByName() method.

thisComponent.drawPage.forms.getByIndex(0)

This method avoids the confusion about which form name to use or quotations… and just calls the first control in the form drawpage, regardless of name. (nearly always the main form) Otherwise, verify the correct form control name while editing the form, selecting menu bar Form / Form navigator, then browse the list of form controls for the form in question, and its control name. (If it is the main form, it should be near the top of the list, under Form)

Sky,
Done. That works, but I’m now getting a Variable not found error on the line: “oReportdoc = thisDatabaseDocument.reportDocuments.getByName(“Job_Schedule_Sheet”)”.
I tried substituting getByName in this line of code with getByIndex and get the same result. Thanks Erik
Here is the the macro for context:
Sub Run_Report()
Dim oForm as Object
Dim oReportdoc as Object
oForm = thisComponent.drawPage.forms.getByIndex(“Customer_Form”)
If oForm.isNew() then
oForm.insertRow()
Else
oForm.updateRow()
Endif
oReportdoc = thisDatabaseDocument.reportDocuments.getByName(“Job_Schedule_Sheet”)
oReportdoc.open()
End sub

Sky,
I changed a few things to try to figure out the problem. The latest version of the macro is shown further below.
With this code, I am now receiving a different error “Property or method not found: drawPage.” at the same problematic line "oForm = thisComponent.drawPage.forms.getByName(“Customer_Form”).

Sub Run_Report()
Dim oForm as Object
Dim oReportdoc as Object
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

As per instruction, this should be

  • getByIndex(0)

This is not a valid form control name. While editing the form, goto menu bar Form / Form Navigator and examine the control list for the correct form control name (“MainForm”??). The name is case sensitive so the form name in the macro must exactly match the form control name. Again, this is not the same name as is listed for the form in the Forms list.

Sky,
The “getByIndex(0)” appears to work.
I’m now getting a Variable not defined error at the line " oReportdoc = thisDatabaseDocument.reportDocuments.getByName(“Job Schedule Report”)"
Erik

Has oReportdoc been declared? - Dim oReportdoc
Try not even using oReportdoc and instead just use

thisDatabaseDocument.reportDocuments.getByName(“Exact Name”).open()

Verify that the report actually exists in the Reports list, and that the name in the macro is exactly as the actual report name.

To help debug try implementing this IF HasByName method in the macro code where you wish to open the report. (replace the last few lines of the macro where oReportdoc is used)

If thisDatabaseDocument.reportDocuments.hasByName("Exact Name") then
thisDatabaseDocument.reportDocuments.getByName("Exact Name").open()
Else
msgBox("A report with that exact name does not exist")
Endif

Of course, substitute your actual report name where I have “Exact Name”

Contributors to this topic may wish to read this information about Markdown for displaying code.


Using blockquote can introduce discrepancies in the way the code is displayed, including converting quotes to ‘smart’ quotes and interpreting some Markdown formatting characters such as the asterisk. For example, this code in blockquote:

SELECT WRT FROM “MYTABLE” WHERE “ID” <> ‘Text’
If thisDatabaseDocument.reportDocuments.hasByName(“Exact Name”) then
thisDatabaseDocument.reportDocuments.getByName(“Exact Name”).open()
Else
msgBox(“A report with that exact name does not exist”)
Endif

would render as below using backticks ```

SELECT *WRT* FROM "MYTABLE" WHERE "ID" <> 'Text'

If thisDatabaseDocument.reportDocuments.hasByName(“Exact Name”) then
thisDatabaseDocument.reportDocuments.getByName(“Exact Name”).open()
  Else
    msgBox(“A report with that exact name does not exist”)
    Endif

Note the way quotes are displayed, lines not wrapped and spacing preserved, to mention just a few differences.

2 Likes

Thanks for the info… I was wandering how this was done but never made time to search it out! Thought I could get by with just blockquotes. I will be using the backticks from now on!

would be even better added here : Macros for LibreOffice Base - The Document Foundation Wiki
since all these back and forth here makes the topic unreadable :face_with_thermometer:

Sky,
Further below is the latest version of the macro to run a report from a button.
It includes all your suggestions including the “debug” code you suggested.
I have also tripple-checked the report name.
When I run the macro, I get a “Basic runtime error. Variable not defined.” error with the code line “If thisDatabaseDocument.reportDocuments.hasByName(“Job Schedule Report”) then” highlighted.

Sub Run_Report()
    Dim oForm as Object
    oForm = thisComponent.drawPage.forms.getByIndex(0) 
    If oForm.isNew() then
       oForm.insertRow()
    Else
       oForm.updateRow() 'If I get an error here its because the form is not displaying data
    Endif
    If thisDatabaseDocument.reportDocuments.hasByName("Job Schedule Report") then
       thisDatabaseDocument.reportDocuments.getByName("Job Schedule Report").open()
    Else
       msgBox("A report with the name 'Job Schedule Report' does not exist")
    Endif
End sub

Added backticks to correctly display code as @nebergall seems not to have noticed my advice above [robleyd]

Hmmm, the only thing else to do is upload a stripped down version of your odb so I can do a hands on debug. We probably should have started with this and saved a lot of fruitless back and forths.