how to access to the controls of a base form with basic ?

I created a form into a LO Base with the WYSIWYG form editor. Next I would like to access to the controls which are inside this form. However the 2nd line of the below code raises the exception “Property or method not found : getByName” (translation of french terms “Propriété ou méthode non trouvée : getByName”).

form = ThisDatabaseDocument.FormDocuments.getByName(“myForm”)
textArea = form.getByName(“Console”)

If you add the MsgBox line as shown below:-

form = ThisDatabaseDocument.FormDocuments.getByName("myForm")
MsgBox form.dbg_properties
textArea = form.getByName("Console")

you will see that the MsgBox shows

Properties of object


so the form object created is not the form but the Form Document Definition.

Change the code to

form = ThisComponent.Drawpage.Forms.getByName("myForm")
MsgBox form.dbg_properties
textArea = form.getByName("Console")

and the MsgBox will show

Properties of object


so the form object is now your Form.

You get the error on your second line as there is no getByName() method for the DocumentDefinition object.

EDITED 29/01/2015

How are you running the Macro? If you run it from a form event such as a button click or from the Toolbar on the Form Tools>Macros>Run Macro it will work. If you run it from the toolbar on the main LO window or from the Macros Window it will fail. ThisComponent or ThisDatabaseDocument points to the Document that has the focus so the Form must have the focus.

There is a quick way of getting the Form Object using the Macro below.

Sub Button(e)
oForm = e.Source.Model.Parent
End Sub

The Macro is run from a Button Click event on a Button on the Form. The Argument (e) is required in the Macro. When run the Argument e is passed to the Macro. The Source of the event e is the Button Object and the Parent of the Button is the Form Object.

The code you suggested fails on the first line with the below error :
“Property or method not found : Drawpage” (translation of french terms “Propriété ou méthode non trouvée : Drawpage”).

@Jérôme I have edited my original reply as it is too long to fit in a comment.

You might find this useful:- Working with Forms - Apache OpenOffice Wiki

The following code works when run from the main Base window. The code is partly from Open form via macro in Libreoffice Base.

    form_container = ThisDatabaseDocument.FormDocuments.getByName("Form1")
    Wait 500
    main_form = form_container.Component.getDrawPage().getForms().getByIndex(0)
    control = main_form.getByName("txtName")