Macro Help Base

Hi everyone

With great help from Ratslinger I learned a bi t while instituting a Macro (to send the current records to a report with the push of a button. But I came across another problem that is beyond me.

I have a few other buttons that launch various reports (also using macros). If I leave 1 macro at a time in my database the targeted buttons work fine. However when I put both macros in, one of them produces an error.

The first macro sends the current records to a report:



REM ***** BASIC *****

Option Explicit

Sub OpenReport

Dim oController As Object

Dim oReportDoc As Object

oController = ThisDatabaseDocument.currentController

if not oController.isconnected then oController.connect

oReportDoc = Thisdatabasedocument.reportdocuments.getbyname(“Email_Form”).open

End Sub



Sub PrintCurrent

Dim oForm As Object

Dim oColumn As Object

Dim iField As Integer

Dim oSubForm As Object

Dim oSubSubForm as Object

Dim oSubSubSubForm as Object

Dim oController As Object

Dim oReportDoc As Object

oForm = ThisComponent.Drawpage.Forms.getByName(“MainForm”)

oSubForm = oForm.getByName(“Contact”)

oSubsubform = osubform.getByName(“details”)

iField = osubsubForm.getByName(“txt-Res ID”).Text

if oForm.isNew() Then

MsgBox “New Record - Cannot Print”

Exit Sub

End If

oSubSubForm = oSubForm.getByName(“PrintForm”)

oColumn = osubSubForm.Columns.getByName(“res_id_fk”)

oColumn.updateInt(iField)

oSubSubForm.updateRow()

oController = ThisDatabaseDocument.currentController

if not oController.isconnected then oController.connect

oReportDoc = Thisdatabasedocument.reportdocuments.getbyname(“Email_Form”).open

End Sub



The second sends various buttons to certain reports



Sub PrintReport(oEvent)

Rem Get name of Control

sControlName = oEvent.source.Model.Name

Rem Check for button pushed

If sControlName = “btn1” Then

ReportName = “Party Order Form Today”

EndIf

If sControlName = “btn2” Then

ReportName = “Party Order Form By #”

EndIf

If sControlName = “btn3” Then

ReportName = “Party List”

EndIf

If sControlName = “btn4” Then

ReportName = “Tip List”

EndIf

If sControlName = “btn5” Then

ReportName = “Counter Weekly”

EndIf

If sControlName = “btn7” Then

ReportName = “Event Invoice”

EndIf

If sControlName = “btn8” Then

ReportName = “Email_Form”

EndIf

If sControlName = “btn9” Then

ReportName = “Corp Order E-Mail”

EndIf

If sControlName = “btn10” Then

ReportName = “Theme Email”

EndIf

If sControlName = “btn11” Then

ReportName= “specOinvoice”

EndIf

oContext = CreateUnoService(“com.sun.star.sdb.DatabaseContext”)

Rem set registered DB name here

oFont = oContext.getRegisteredObject(“Reservations JDBC”)

dbForms = oFont.DatabaseDocument.ReportDocuments

oAConnection = oFont.getConnection("","")

Dim pProp(1) As New com.sun.star.beans.PropertyValue

pProp(0).Name = “ActiveConnection”

pProp(0).Value = oAConnection

pProp(1).Name = “OpenMode”

pProp(1).Value = “open”

oForm = dbForms.loadComponentFromURL(ReportName, “_blank”, 0, pProp())

End Sub

When I put both macros in one after another I get a Variable Not Defined error on this line. What is the correction needed?

sControlName = oEvent.source.Model.Name





Thanks, Jody
W10, LO v7.1.2.2

Hello,

Deleted the comment. Found the real problem.

At the very top you have defined Option Explicit.

With this you need to Dim all your variables. You did this in the first two Sub’s but none in the third. Since the first line of code had the variable undefined (no Dim statement) it generated the error.

Edit:

Dim sControlName As String
Dim ReportName As String
Dim oContext As Object
Dim oFont As Object
Dim dbForms As Object
Dim oAConnection As Object
Dim oForm As Object

Add a beginning of third macro after Sub

Did minor test only because not set with names you are using.

As I am a real newb at this, what are the variables that have to be Dim’d?

Have you looked at the documentation concerning macros? You need to get some of these basic terms and what the are/do or you will not progress.

LibreOffice Base Guide

Edit:

On the same page as the Base guide and toward the top are the Getting started guides. There is one there for macros.

And for further depth in macros you may want to look at Open Office Macros Explained by Andrew Pitonyak. PDF here → OOME

and

There is more.

Starting to read today