Page 1 of 1

[Solved] Open Base Report from Writer

PostPosted: Thu Mar 26, 2015 12:35 pm
by fuzzyluzzi
I've seen this code for opening a report. It works well if run directly from the database. How would I change it to use an active connection with a registered Database from a Writer document.

Thanks

Code: Select all   Expand viewCollapse view
Sub OpenMyReport(oEvent As Object)
Dim RptName as string
RptName = "Date Query"
'the name of the report I want to open
ThisDatabaseDocument.ReportDocuments.getByName(RptName).open
End Sub

Re: Open Base Report from Writer

PostPosted: Thu Mar 26, 2015 4:06 pm
by Villeroy
The following code has mostly been recorded using the MRI object inspection tool.
[Tutorial] Introduction into object inspection with MRI
The code can open any report from any form control that is attached to any form connected to any data source. Just add the name of your report to the form control's "Additional Info". In your particular case you set "Date Query" (without quotes) as "Additional Info" of your push button.
Code: Select all   Expand viewCollapse view
Sub OpenReportByTag(oInitialTarget As Object)
  Dim oSource As Variant
  Dim oModel As Variant
  Dim oParent As Variant
  Dim oActiveConnection As Variant
  Dim oParent2 As Variant
  Dim oDatabaseDocument As Variant
  Dim oReportDocuments As Variant
Dim sRepName, oReport
  oSource = oInitialTarget.Source
  oModel = oSource.getModel()
sRepName = oModel.Tag
  oParent = oModel.getParent()

  oActiveConnection = oParent.ActiveConnection
  oParent2 = oActiveConnection.getParent()
  oDatabaseDocument = oParent2.DatabaseDocument
 
  oReportDocuments = oDatabaseDocument.getReportDocuments()

oReport = oReportDocuments.getByName(sRepName)
oReport.open()
End Sub

So far this code finds the correct database document and the correct report document therein. It does even open the requested report but it won't be filled with data unless I have visible instance of that database with its own active connection (one click on the tables section connects the database). The form's connection is not able to fill the report. This is beyond me. I don't have the time to get behind this. I'm happy with my Calc reports that work out of the box. These things used to work so much better in OpenOffice 1.x when there were no Base documents and all forms and reports were stand-alone Writer documents.

Re: Open Base Report from Writer

PostPosted: Fri Mar 27, 2015 6:38 am
by fuzzyluzzi
so because there is no active connection, which is the error I get, I cannot run this report from anywhere but the actual database file. lame.

Re: Open Base Report from Writer

PostPosted: Sat Mar 28, 2015 4:09 pm
by Villeroy
I'm 90% sure it can be done but I don't have the time nor patience to find out how.

Re: Open Base Report from Writer

PostPosted: Sat Mar 28, 2015 9:02 pm
by F3K Total
Villeroy wrote:I'm 90% sure ...

now you can be 100% sure ...
Code: Select all   Expand viewCollapse view
Sub OpenReportByTag(Event As Object)
    Dim oParms(0) As New com.sun.star.beans.PropertyValue
    oButton = Event.Source.Model
    sRepName = oButton.tag
    oParent = oButton.Parent
    oActiveConnection = oParent.ActiveConnection
    oDatabaseDocument = oActiveConnection.Parent.DatabaseDocument
    oReportDocuments = oDatabaseDocument.ReportDocuments
    oParms(0).name = "ActiveConnection"
    oParms(0).value = oActiveConnection
    ocomponent = oReportDocuments.loadComponentFromURL(sRepName, "", 0, oParms())
End Sub

R

Re: Open Base Report from Writer

PostPosted: Sat Mar 28, 2015 10:36 pm
by Villeroy
Thank you. How could I find out that "ActiveConnection" can be used as a property argument to loadComponentFromURL?

Re: Open Base Report from Writer

PostPosted: Sat Mar 28, 2015 11:15 pm
by F3K Total
here? (details)
... but i just copied the code from somewhere else ...
R

Re: [Solved] Open Base Report from Writer

PostPosted: Mon Mar 30, 2015 10:35 am
by fuzzyluzzi
Thanks. That opened it perfectly. Would it be possible to modify this macro to automatically save/export the report or should I do that from inside the report?

Re: [Solved] Open Base Report from Writer

PostPosted: Mon Mar 30, 2015 12:18 pm
by Villeroy
It's a Writer document. Click the PDF button on the standard toolbar.