Which event is triggered when the user closes a BASE form using the X in the top right hand corner?

Hi Everyone

Still a beginner here …

I have some base code that I want to execute when a user closes a form by checking the close window X at the top right of a BASE form. My intention is to close the BASE application when the user closes the form. Which event is that? I cannot see an ‘on Close’ event associated with the form itself.

I’m trying to follow NextureJohn in post 49147 but am having problems closing down the application when the user closes the form…

Thanks for your help,

CB

I have tried this: Close Base when last form has been closed. But it won’t run well under Windows, if Base is last application of LO and LO should also be closed.

Open Base → Tools → Customize → Events → Closed a sub component

SUB DatabaseClose1
	DIM oDispater AS OBJECT, oDoc AS OBJECT, oFrames AS OBJECT 
	DIM i AS INTEGER, k AS INTEGER, n AS INTEGER
	oDoc = ThisDatabaseDocument
	IF LBound(oDoc.CurrentController.SubComponents()) > UBound(oDoc.CurrentController.SubComponents()) THEN
		oFrames = StarDesktop.Frames
		n = oFrames.Count
		FOR i = 0 TO n - 1
			IF InStr( oFrames.getByIndex(i).Title, oDoc.Title) THEN
				k = k + 1
			END IF
		NEXT i
		IF k = 1 AND n = 1 THEN
			DataWrite			
			oDoc.store()
		'	Wait(500)
			REM https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1frame_1_1XDesktop.html
			' StarDesktop.terminate()
		'	oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
			REM .uno:CloseWin, .uno:CloseDoc, .uno:Quit
		'	oDispatcher.executeDispatch(oDoc.CurrentController.Frame, ".uno:Quit", "", 0, Array())
		ELSEIF k = 1 AND n > 1 THEN
			DataWrite
			Wait(500)
			oDoc.Close(True)
		ELSE
		END IF
	END IF
END SUB

You will see: I have only activated closing Base if there is another window opened. And: It won’t close if you have macro editor open.

Simply create a stand-alone form document. This can be opened just like a normal document without Base window.

  1. Save the form document as a Writer document.
  2. Close the form.
  3. Open the document and turn on form edit mode.
  4. Open the form navigator and walk through all forms and subforms and do this with each of them:
    4.1. Copy the source item.
    4.2. Specify either the Base document’s registered name (from drop-down) or the path to the Base document (click the ellipsis button for a file picker).
    4.3. Now the source item is removed but you have it in clipboard.
  5. File>Properties… tab “Security” and check “Open Read-Only” (just like the embedded form).

Thank you both for your responses, they are much appreciated!

Since most of my users will be using Windows, I decided to follow Villeroy’s instructions first (sorry RobertG!). Bearing in mind that this was the first time I had ever loaded a Writer document, I followed Villeroy’s instructions to the letter.

What the instructions seem to create is a standalone form which references but does not open the source item, and therefore all the macros associated with the ‘On Open’ event of the source item now run after the form connects with the remote database rather than before.

I understand more now why you recommend that you let the application handle the login with the remote database ( see my previous entry here: 113764). I will change the way the user logs in and report back.

CB

Regarding StarBasic macros:

  1. Store the macros under “My Macros” or store them in the form document. In the macro organizer (tab “Libraries”) you can export your library to an extension which installs your library under “My Macros” on other machines. This can not work with the “Standard” library. The library nees to have some unique name. It is possible to build an extension that includes your database with registration, the macro library and stand-alone form documents. I don’t know how to do this.

If your macros refer to the stand-alone form, you should embed the macros in the form document.
2. Replace the global variable ThisDatabaseDocument (the embedding odb which is no longer loaded) with ThisComponent, which is either the currently active office document or the embedding office document.

The following code is far more portable than ThisComponent.DrawPage.Forms.getByName("MyForm").getByName("MyControl")

Sub Some_ControlEvent(ev)
oControl = ev.Source
oForm = oControl.Parent
REM if the control is embedded in a table control:
REM oForm = oControl.Parent.Parent
[...]
Sub Some_FormEvent(ev)
oForm = ev.Source

The following function returns any parent object of some given object:

Function getParentObject(byval m, srvName$)
on error goto NullObj
	do until m.supportsService(srvName)
		m = m.getParent()
	loop
	getParentObject = m
	exit function
	NullObj:
	getParentObject = Null
End Function

getParentObject(ev.Source, "com.sun.star.form.component.DataForm") gets the calling object’s form.
getParentObject(ev.Source, "com.sun.star.document.OfficeDocument") gets the calling objet’s embedding document.

Hi Everyone - and VilleRoy

First of all, Thanks VilleRoy for all your advice and comments…

I tried saving the form as a Writer document, but the standalone form kept opening first, whereas I wanted it to open dependent on a login (and with the labels displayed in the language of the user).

In the end, I fell back on the solution found in TextureJohn 49147 but using a function derived from another solution by Ratslinger in 66707 .

I created a function to test to see if a form name is open:

Function IsOpenForm(formName as String) as Boolean

Dim oFrames as Object
Dim oForm as Object
Dim sTitle as string
Dim i as integer

IsOpenForm = False
oFrames = ThisComponent.getCurrentController().getFrame().getFrames()

For i = 0 to oFrames.Count  - 1
    oForm = oFrames.getByIndex(i)
    sTitle = oForm.Title
    If InStr(sTitle,formName)>0 then 
       IsOpenForm = True
    End if    
next i

End Function

This can be used in place of the Continue variable in the solution of 4917, as in:

do while IsOpenForm(“myFormName”)
wait 20
loop
ThisComponent.close(true)

The function is stored in the application’s library, as are all other custom functions and dialogs.

Best wishes,

CB

: