Clean closedown of LibreOffice when Form closes

As noted in my comment, Base is not “Web” oriented. Anyone accessing your data will do it using an IP and not the browser. Also they need to have LO installed on their system along with your specific form and queries if designed that way. They connect to your server to get the data and nothing else.

All this about closing everything is in the hands of the user. Only item on your part is if someone is connected to the server accessing the data. You simply have the database.

Wow, that is most unfortunate, I seem to have made a fundamental design mistake, requiring a serious rethink.
I would though like to at least make my application available as a stand-alone program that can run on a PC.
So to do that, I still need to solve the questions listed at the beginning of this post.

Assume a stand-alone PC, a non-expert user of the program, who clicks on a button and runs my .odb.
Using the Sample 2 example posted by Mr Ratslinger (link), the LibreOffice GUI disappears but leaves the Form running, which the user can utilise. This is what I want.
But Ratslinger says:
"IMPORTANT! If you just close the form, the .odb is still active in memory! To Exit properly, use Ctrl & Q keys OR from Instruments menu select File->
Can a macro be written to do this automatically when the form is closed “brutally”, for example by pressing the X in the top right hand corner?

Have you looked at the link provided in the earlier comment? That is essentially running a form without opening the .odb and not needing to worry about closing a hidden version.

Simple way to go.

No, as it talked about a Writer document, which did not seem to be my case. I’ll look at it now, thanks

Yes that is correct. You may not realize this but a Base form is basically a Writer document. You can use a Writer document as a form connected to a Base file. The actual .odb need not be opened. This makes it fit in with your request.

Hi, unfortunately in my case the solution proposed using Writer does not work, due to the fact that my Form uses macros. So I am back to my original question: is it possible to write a macro to cleanly close Base when using the Sample 2 described by Ratslinger?

Not sure why that is a problem. Have used macros in standalone forms many times.


Have removed suggested code as further testing with the hidden Base files does create problems.

Concerning the standalone Forms, which do seem the cleanest solution, and your observation that you have used macros in standalone forms many times, it seems strange that in my case the Form doesn’t work. First of all I got errors that it didn’t find the macro(s). So I copied and pasted the macros into the Form. Now it just doesn’t find any data, so probably it is not connecting to the database. I have tried going back to an old version with an embedded database, to see if that helps, but it still doesn’t find the data.

Sorry to keep posting, I may have found the solution, by manually speicfying the .odb file file as the source. Watch this space!

ok, getting there, the key is to specify the .odb file as the Data Source for all the forms and subforms. The standalone Form will though not work with the temporary tables, as the Form cannot find them in the .odb file, even if they are there! The Form also prompts me for the password to connect to the Firebird databases, so the connect_to_database macro will need looking at. So it looks to me that if I want a standalone form I will need to go back to a permanent file tbl_SrchValues to exchange data (no problem as the Form will be standalone) and to an embedded database (again no problem for the standalone Form).


This seems to work OK although I prefer to use the standalone forms.

Create a global variable:

global Continue as boolean

At the beginning of the Sub ‘HideDBWinOpenSwitchboard’ add:

Continue = true

Just before the end of the Sub ‘HideDBWinOpenSwitchboard’ add:

do while Continue
	wait 20

Add this sub:

Sub CloseBase
    Continue = false
End Sub

and link it to the Document Closed event of the form you are dealing with.

When that form is closed it will close that Base file.


Can all the code that follows be safely excluded in my case?

No. That is code to open the specific form. You must specify your form to be opened there. Of course, the commented lines can be eliminated.

Hi, I have tried the code you suggest, but on my Form it crashes LibreOffice: the Form comes up and shows the correct initialisation, but then will not respond to any input, and it crashes. If I comment out your “do while loop” then it works normally. So having a loop active in the Open Document event in my case does not seem to be a solution. I will try your suggestion of eliminating the temporary hidden table and see if that works.

Isn’t there anything that can be done just by invoking the “Document Closed” event? Thanks

Have tested this process without problem as you note. Would need to see the code you actually used.

Isn’t there anything that can be done just by invoking the “Document Closed” event? Thanks

No as the functions for the code needed is not available since the form is closed. And if done before actually closing the form it causes other problems such as crashing Base.

I have added the macro code into the original question above. It is 99% the same as yours.
By the way, I have nearly got the standalone Form to work now, just having a few problems with the emporary tables and connecting to the Firebird database (see comments just before your Answer above)

Have tested with one of your forms and your posted code. Have no problems. However, with further checking, although it completely closes the open Base file, LO is still open. A complete shutdown is not possible as there is a problem with that. See → Clean exit macro from DB and LibO ?.

I have not seen an easy solution. Here is one presented by killing the task with a shell command →
Re: Closing OpenOffice by macro

May someday test this but would need to set up a separate install to prevent damage to the system in event of problems.

OK so someday was today. This (for Linux Ubuntu 18.04):

Shell "killall soffice.bin"

just before End Sub of HideDBWinOpenSwitchboard will remove the remainder of LO. However you must understand that any LO items currently open (at this point the Base file is already closed) will be killed and can damage the files.

You can test the killall command in a terminal session.

Thanks for this, I think it is time to call it a day on this discussion.
The “do while loop” on my system crashes it, and the risk of damage using “killall” is too high.
So I am going to do the following:

  1. for use by myself on a PC or on Lubuntu, I will use your Sample 1 code, which automatically runs my Form full-screen, with LO hidden behind it. I will then make sure that everything is closed down properly by hand.

    Sub openForm
    ’ derived from Ratslinger Sample 2
    ’ macro bound to Open Document Event (see Customize - Events - Open Document)
    ’ run once when .odb file is opened
    Dim oForm As Object
    oForm = ThisDatabaseDocument.FormDocuments.getByName(“name of form”).open
    oForm.CurrentController.Frame.ContainerWindow.IsMaximized = true
    End Sub

  1. for use in the archives with non-skilled people accessing my Form on a PC, I will make a Writer Standalone Form, and modify my system to not have temporary tables, and for the database to be embedded. This will solve the problems currently being discussed in the other thread (link).

I have cleaned up my original question so that the text is consistent and useful for other readers.

Thankyou so much Ratslinger for all your support and help.

Finally, as I still have not solved my original design problem of using my Form in an internet shared environment, because, as you have pointed out, this is a design error on my behalf, I wonder if you comment on the possibilty of using LibreOffice Online or Collabora to allow my LibreOffice form to be run in a browser (or do I have to open another question?)

To my understanding LO Online does not include Base or any connection to it. Using a browser requires a DB front end other the Base. Have no suggestions for that and is outside the scope of this site.