Inserting an SQL command into the contents of a report

How to insert an SQL command into the contents of a report. So far I’ve arrived at this code:

Sub ReportTest
sDBName = "file:///home/davi/Documentos/Demanda_Espont%C3%A2nea.odb"
    oContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
    oDB = oContext.getByName(sDBName)
    oConn = oDB.getConnection("","") 
    oParent = oConn.getParent()

    oDatabaseDocument = oParent.DatabaseDocument
    oReport = oDatabaseDocument.reportdocuments.getbyname("MyReport")
    oReport.commandType = 2
    oReport.command = "SELECT  ..."
    oReport.execute()
    oReport.reload()
End Sub

In the line “oReport.commandType = 2” the message “Property or method not found: commandType” appears.
What should I do?

Hello,

You stated:

In the line “oReport.commandType = 2” the message “Property or method not found: commandType” appears. What should I do?

Best to start learning MRI or XRay object inspector.

The problem here is that at:

oReport = oDatabaseDocument.reportdocuments.getbyname("MyReport")

There is no commandType or command property at this point.

These properties are available when the report is in the edit mode. You can edit the report with a macro but not through the connection you have. For this you should have the object sent from the form. Have given basics of this before.

Tested with:

Sub sendObject
    if IsNull(ThisDatabaseDocument.CurrentController.ActiveConnection) then
        ThisDatabaseDocument.CurrentController.connect
    endif
    LimparCampos2(ThisDatabaseDocument)
End Sub

Then the code in MyMacros (guessing that is entire objective here):

Sub LimparCampos2(oDBdoc) As Object
    oReport = oDBdoc.reportdocuments.getbyname("MyReport")
    oRDesign = oReport.openDesign
    oContainerWindow = oRDesign.getCurrentController().getFrame().getContainerWindow()
REM Can set design to minimized or not Visible
  REM  oContainerWindow.IsMinimized = True
  REM  oContainerWindow.IsMinimized = False
      oContainerWindow.setVisible(False)
  REM  oContainerWindow.setVisible(True)
    oRDesign.CommandType = 2
    oRDesign.Command = "YOUR SQL HERE"
    oReport.open
    oReport.close(True)
End Sub

You should add DIM statements. Design mode closes at end of sub but report itself stays open. Report design remains as before processing.

Edit 2021-07-10:

Recalled access method using your original connection (had done this on a standalone Writer form):

Sub OpenRpt
    sDBName = "DB Name Or Location"
    oContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
    oDB = oContext.getByName(sDBName)
    dbForms = oDB.DatabaseDocument.ReportDocuments
    oConn = oDB.getConnection("","") 
    ReportName = "Report Name"
    Dim pProp(1) As New com.sun.star.beans.PropertyValue 
    pProp(0).Name = "ActiveConnection" 
    pProp(0).Value = oConn
    pProp(1).Name = "OpenMode" 
    pProp(1).Value = "openDesign" 
  REM Open in Design to modify
    oReport = dbForms.loadComponentFromURL(ReportName, "_blank", 0, pProp()) 
    oContainerWindow = oReport.getCurrentController().getFrame().getContainerWindow()
  REM  oContainerWindow.IsMinimized = True
  REM  oContainerWindow.IsMinimized = False
    oContainerWindow.setVisible(False)
  Rem  oContainerWindow.setVisible(True)
    oReport.CommandType = 2
    oReport.Command = "YOUR SQL"
    Dim pProp2(1) As New com.sun.star.beans.PropertyValue 
    pProp2(0).Name = "ActiveConnection" 
    pProp2(0).Value = oConn
    pProp2(1).Name = "OpenMode" 
    pProp2(1).Value = "open" 
  REM Open while Design not yet closed - has SQL
    oReport2 = dbForms.loadComponentFromURL(ReportName, "_blank", 0, pProp2()) 
    oReport.getCurrentController().getFrame().Close(True)
End Sub

Still requires editing report but now uses your original connection code.

Thank you so much Ratslinger!I am very grateful for your contributions to my questions, you have helped me a lot. Do you have any material suggestions for studying the use of MRI?

Do you have any material suggestions for studying the use of MRI?

Have presented in past link to Base learning materials. One of the links in that post was for MRI tutorial. Here is the direct link to that tutorial:

https://forum.openoffice.org/en/forum/viewtopic.php?f=74&t=49294

Also the latest MRI versions (I have v1.3.4 in use on Ubuntu 20.04):

More MRI info: