Page 1 of 1

[Solved] Running a script from a button

PostPosted: Fri Sep 27, 2019 9:47 am
by gkick
Hello,

In order to strip the db of testdata and reset counters etc I use a textfile containing multiple SQL Statements
while the copy/paste into Tools/SQL works fine, I do not seem to be able to run this from a button but it also works fine when run from the macro window.

Any ideas what I am doing wrong here?

:?

Re: Running a script from a button

PostPosted: Fri Sep 27, 2019 2:43 pm
by FJCC
I am not familiar with Base macros but perhaps ThisComponent does not refer to the intended object when you run the code from the button. You can use the extension MRI to inspect what ThisComponent is in each case. There is a tutorial about MRI.

Re: Running a script from a button

PostPosted: Fri Sep 27, 2019 4:05 pm
by Villeroy
viewtopic.php?f=21&t=95587
Install the Python macro
Create a spreadsheet
Add a named cell "DB_Name" and enter the registered name
Add a named cell "SQL_Statement" and enter your DELETE statements separated by semicolons in correct order of dependencies (foreign keys before primary keys).
The macro to be called is "My Macros" > pyDBA > ParamsFromSheet > Main

A chain of DELETE statements does not require any further setup.

Re: Running a script from a button

PostPosted: Fri Sep 27, 2019 4:06 pm
by gkick
Thank you, will check it out.

Re: Running a script from a button

PostPosted: Fri Sep 27, 2019 5:09 pm
by UnklDonald418
Try using the Approve Action event for the Button to get the active connection
Code: Select all   Expand viewCollapse view
sub runcleanup (oEv as Object)

oDoc = oEv.Source.Model
While oDoc.ImplementationName <> "com.sun.star.comp.dba.ODatabaseDocument"   
  oDoc = oDoc.Parent
Wend

REM  now get active connection
oConn =   oDoc.CurrentController.ActiveConnection
oStatement = oConn.createStatement()

Re: Running a script from a button

PostPosted: Fri Sep 27, 2019 5:55 pm
by gkick
Thank you all for your help, changing the event to Approve Action did do the trick, thats great !!!!

Re: Running a script from a button

PostPosted: Fri Sep 27, 2019 5:56 pm
by Villeroy
The screenshot indicates that the macro to be run is embedded in the database document. Replace ThisComponent with ThisDatabaseDocument which refers to the containing database document from the view point of any embedded object.