Open form directly when opening a database

Version: 7.5.7.1 (X86_64) / LibreOffice Community
Build ID: 47eb0cf7efbacdee9b19ae25d6752381ede23126
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: pt-BR (pt_BR.UTF-8); UI: pt-BR
Calc: threaded

Good morning, I’m building a database that will be used by an user that isn’t me, I would like to make the databse user friendly, and limit the acces the user has to the “backend”. My idea to improve this concept is to make it, in a way, that when the user clicks to enter the database, it automatically opens the forms, where they’re supposed to work. I don’t know how to start building this macro, or if theres another way to do it, like, creating some shortcut that leads towards the form. Any help is welcome.

You only need something like this, saved in macros of the database file:

SUB FormOpen
ThisDatabaseDocument.FormDocuments.getByName("stForm").open
END SUB

Connect this in main screen of Base to Tools → Customize → Events → Open Document.

PS: Sorry, there is something wrong with that Python macro. Use the original Basic code instead:
Apache OpenOffice Community Forum - FreeHSQLDB v.0.4 - (View topic) in order to save all forms as stand-alone documents
or do it manually:

  • open, save as Writer document and close.
  • open the saved document and reconnect all forms and subforms to the database.
  • make the document or the file read-only.

when I try running the macro it gives me an error saying that “There is no connection to the database”

@samuel_ribeiro : Where did you save the macro? Should be inside the database file. Then it could be started from the database (for testing) and started in the events of the database file as described. Connection to the database will be established when opening the form.

@RobertG : I think its all in the correct place, but i keep getting the error.

The macro is inside the document

Captura de tela_2023-12-20_17-20-44

This is the macro modified to my form name

Captura de tela_2023-12-20_17-20-51

And it is linked to the “On open document” event and saved to “fraCOPA.odb” that is my database name.

The error I’m getting

Captura de tela_2023-12-20_17-19-36

Is there something I’m missing?

Then Try to set this:

SUB FormOpen
    oDatasource = thisDatabaseDocument.CurrentController
	IF NOT (oDatasource.isConnected()) THEN oDatasource.connect()
	oConnection = oDatasource.ActiveConnection()
    ThisDatabaseDocument.FormDocuments.getByName("stForm").open
END SUB

Connects first to the database and then opens the form.

The error message tells it all. This macro tries to connect the database before opening anything.
Pass the odb document, the hierarchical name (forms and reports may be organized in folders) and if the hierarchical name refers to a report or not.

Sub OpenEmbedded(odb, sHierachicalName, bReport As Boolean)
	if bReport then
		container = odb.ReportDocuments
	else	
		container = odb.FormDocuments
	endif
	oCtrl = odb.CurrentController
	IF NOT (oCtrl.isConnected()) THEN oCtrl.connect()
	obj = container.getByHierarchicalName(sHierachicalName)
	obj.open()
End Sub

I’m still against all this dysfunctional macro madness.

P.S. I tested the above code with a password protected account of an external HSQLDB (menu:Edit>Database…Properties, Password required=Checked).
When there is no connection because the document has been opened, but no data have been accessed so far, the application prompts for the password automatically.