Page 1 of 1

[Solved] AUTORUN macro to open a LO Base form

Posted: Fri Apr 16, 2021 6:21 am
by dmb1319
I am very new to LO, trying to break the microsoft paradigm.

In a MSAccess database, I created a simple macro and named it AUTORUN.
When I double clicked the database file name or assigned a it to a shortcut and clicked it, the AUTORUN macro ran and opened a database form.

In LOBase I had to look around and fnally found macros under the Tools menu. There, I'm not exactly sure what I am looking at. It LOOKS a little like a Microsoft VB editor. Is this all there is in LOB? I have some very limited programming skills. Do I have to learn LOBasic to create a simple autorun macro to open a database form when the database is run?

I did see something about connecting a Writer document to the database form and creating a shortcut to the Writer document to open the form. I am sure I must have misunderstood that. It just doesn't make sense.

Thank you for any help anyone can give me on this.

Re: AUTORUN macro to open a LO Base form

Posted: Fri Apr 16, 2021 11:09 am
by Villeroy
OpenEmbedded can open any embedded form or report by passing the database document, the hierarchical name and a boolean value.
sHierarchicalName: "MyForm" or "Subfolder/My Form" (embedded forms and reports can be organized in folders)
bReport: False to address a form, True for a report.
The 2 _Button routines are examples for push buttons keeping the hierarchical names as "additional info" property. If the code is embedded in a database document, ThisDatabaseDocument refers to the embedding document.

Code: Select all

Sub Open_Report_Button(e)
REM specify the hierarical name in the button's "Additional info" field
	sName = e.Source.Model.Tag
	OpenEmbedded(ThisDatabaseDocument, sName, bReport:=True)
End Sub

Sub Open_Form_Button(e)
REM specify the hierarical name in the button's "Additional info" field
	sName = e.Source.Model.Tag
	OpenEmbedded(ThisDatabaseDocument, sName, bReport:=False)
End Sub

Sub OpenEmbedded(odb, sHierachicalName$, bReport As Boolean)
	if bReport then
		container = odb.ReportDocuments
	else	
		container = odb.FormDocuments
	endif
	obj = container.getByHierarchicalName(sHierachicalName)
	obj.open()
End Sub
I can offer another macro which saves all contained forms of a document as stand-alone form documents so you can open the forms without Base document. That macro is contained in FreeHSQLDB v.0.3. Sooner or later you will extract an embedded HSQLDB anyway.

Re: AUTORUN macro to open a LO Base form

Posted: Fri Apr 16, 2021 3:38 pm
by Villeroy
Of course you have to learn programming before you program.
Install my FreeHSQLDB extension.
Tools>Macros>Basic... [Alt+F11] > MyMacros > FreeHSQLDB > FreeForms > Main, [Run] extracts the forms into the same database directory, mirroring any subfolder structure of the forms container. It is a good idea to register the database in Tools>Options>Base>Databases before running the macro. The macro extracts everything from the forms container keeping the original forms in place. Delete what you don't want/need.
-------------------------------------
How to use my macro to open the embedded form:
1) Tools>Options... Security, button [Macro Security...] Level "Very High", tab "Trusted Sources" and add one or more directories for documents calling any macro code.
2) Open your Base document
3) Tools>Macros>Basic... [Alt+F11], select your database document and click [New...]
4) Add the following code to the new module embedded in your database document:

Code: Select all

Sub AutoOpen()
OpenEmbedded(ThisDatabaseDocument, "MyForm", False)
End Sub

Sub OpenEmbedded(odb, sHierachicalName$, bReport As Boolean)
       if bReport then
          container = odb.ReportDocuments
       else   
          container = odb.FormDocuments
       endif
       obj = container.getByHierarchicalName(sHierachicalName)
       obj.open()
End Sub
5) Replace the word "MyForm" with the actual name of your form in double-quotes.
6) Back to the database window, Tools > Customize... tab:Events, pick "Open Document", click [Macro...] navigate to Your_DB_Doc > Standard > Module1 > AutoOpen, [OK]
7) Save the database document to a trusted directory as specified in step 1.
The "Open Document" entry in Tools > Customize... tab:Events calls the specified macro named "AutoOpen" when the document is opened. Sub AutoOpen has only one line calling sub OpenEmbedded with 3 arguments. The database document (could be any other as well), the name of the form (may be hierarchical) and False (which means "not a report").
 Edit: To the casual reader who is unfamiliar with MS Access 
In addition to VBA, MSAccess has a GUI which allows mapping of predefined events to predefined actions. Predefined events are like our events in various event dialogs. Predefined actions are very similar to our dispatches. MSA offers a dialog where you can pick some event like "Database Open" with a predefined action like "Open Form" with the form name picked from a third listbox.