SFDocuments.Form service

The Form service provides methods and properties to manage forms in LibreOffice documents. This service supports forms in Base, Calc and Writer documents and allows to:

tip

The SFDocuments.Form service is available from LibreOffice 7.2 onwards.


Forms are usually used in LibreOffice documents to create user interfaces connected to relational databases. Hence, the Form service provides quick access to the linked database through the SFDatabases.Database service.

tip

The SFDocuments.Form service is closely related to the SFDocuments.FormControl service.


Definitions

FormDocument

Forms are usually created in Base documents, but they can be added to Writer and Calc documents as well.

In Base, each form you create using the Insert - Form functionality or through the Form Wizard is actually a FormDocument that can be handled with the Form service. Base documents can contain an unlimited number of form documents.

Below is an example showing the hierarchy of all the elements involved in accessing forms and subforms in a Base document. Suppose you have a Base file named Employees.odb and inside it you created a form document to add new employees to the database. The form document contains a main form named EmployeeData that gives access to a table. There is also a subform WorksAtPlant that allows you to associate the new employee to one of the plants of the company.


    Employees.odb (Base document)
     |
     |-- AddEmployee (FormDocument)
          |
          |-- EmployeeData (Main Form)
               |
               |-- WorksAtPlant (SubForm)
  
note

A FormDocument can be seen as a set of forms that provide access to datasets such as database tables and queries from within LibreOffice documents. The names of forms and subforms inside a FormDocument can be accessed using the Form Navigator.


Forms and Subforms

A form document is composed of one or more forms which, in turn, may also contain any number of subforms. A Form is an abstract set of controls that are linked to a specified data source, which can be a database table, a query or a SQL SELECT statement.

In Calc and Writer documents, each form can be linked to datasets located in different databases. On the other hand, in Base documents the database contained in the document is common to all forms.

tip

To invoke the SFDocuments.Form service refer to the methods Forms(), FormDocuments() and OpenFormDocument() of the SFDocuments.Document service


Oppkall av tenester

In Writer documents

The code snippet below shows how to access the form named Form1 that is inside a Writer file:


      Dim oDoc As Object, myForm As Object, ui as Object
      Set ui = CreateScriptService("UI")
      Set oDoc = ui.OpenDocument("~/Documents/MyForm.odt")
      Set myForm = oDoc.Forms("Form1")
   
I Python

     from scriptforge import CreateScriptService
     svc = CreateScriptService('UI') 
     doc = svc.OpenDocument('~/Documents/MyForm.odt')
     my_form = doc.Forms('Form1')
   

Forms can be accessed by their names or by their indices, as shown below:


     Set myForm = oDoc.Forms(0)
   
I Python

     my_form = doc.Forms(0)
   
warning

If you try to access a FormDocument that is currently opened in Design Mode an exception will be raised.


In Calc documents

A form in a Calc file must have a unique name inside its sheet. Hence, the Forms method requires two arguments, the first indicating the sheet name and the second specifying the form name.


      Dim oDoc As Object, myForm As Object, ui as Object
      Set ui = CreateScriptService("UI")
      Set oDoc = ui.OpenDocument("~/Documents/MyForms.ods")
      Set myForm = oDoc.Forms("Sheet1", "Form1")
   

This is achieved identically using Python:


     svc = CreateScriptService('UI')
     doc = svc.OpenDocument('~/Documents/MyForms.ods')
     my_form = doc.Forms('Sheet1', 'Form1')
   

In Base documents

A FormDocument inside a Base document is accessed by its name. The following example opens the form document named thisFormDocument and accesses the form MainForm:


      Dim oDb As Object, myForm As Object
      Set oDb = CreateScriptService("SFDocuments.Document", ThisDatabaseDocument)
      ' The statement below is necessary only if the form hasn't been opened yet
      oDb.OpenFormDocument("thisFormDocument")
      Set myForm = oDoc.Forms("thisFormDocument", "MainForm")
      ' Or, alternatively, to access the form by its index ...
      Set myForm = oDb.Forms("thisFormDocument", 0)
   
note

To perform any action on a form using the Form service, the FormDocument must have been opened either manually by the user or programmatically in a user script. The latter can be done by calling the OpenFormDocument method of the Base service.


To access a given subform of a form use the SubForms method. Note that in the example below mySubForm is a new instance of the Form service.


     Dim mySubForm As Object
     Set mySubForm = myForm.SubForms("mySubForm")
   

Previous examples translate in Python as:


     db = CreateScriptService('SFDocuments.Document', XSCRIPTCONTEXT.getDocument())
     #  The statement below is necessary only if the form hasn't been opened yet
     form_doc = db.OpenFormDocument('thisFormDocument')
     form = form_doc.Forms('thisFormDocument', 'MainForm')
     #  Or, alternatively, to access the form by its index ...
     form = form_doc.Forms('thisFormDocument', 0)
     sub_form = form.SubForms('mySubForm')
   

In Form events

To invoke the Form service when a form event takes place:


      Sub OnEvent(ByRef poEvent As Object)
          Dim myForm As Object
          Set myForm = CreateScriptService("SFDocuments.FormEvent", poEvent)
          '(...)
      End sub
   
I Python

     def OnEvent(event: uno):
         form = CreateScriptService('SFDocuments.FormEvent', event)
         pass
   
note

The FormEvent service is used exclusively to create instances of the SFDocuments.Form and SFDocuments.FormControl services when a form or control event takes place.


It is recommended to free resources after use of the Form service.


     myForm.Dispose() ' Basic
   

     form.Dispose()  # Python
   

This operation is done implicitly when a form document is closed with the CloseFormDocument() method described below.

Properties

Name

Skriveverna

Type

Beskriving

AllowDeletes

No

Boolean

Specifies if the form allows to delete records.

AllowInserts

No

Boolean

Specifies if the form allows to add records.

AllowUpdates

No

Boolean

Specifies if the form allows to update records.

BaseForm

Yes

String

Specifies the hierarchical name of the Base Form containing the actual form.

Bookmark

No

Variant

Specifies uniquely the current record of the form's underlying table, query or SQL statement.

CurrentRecord

No

Long

Identifies the current record in the dataset being viewed on a form. If the row number is positive, the cursor moves to the given row number with respect to the beginning of the result set. Row count starts at 1. If the given row number is negative, the cursor moves to an absolute row position with respect to the end of the result set. Row -1 refers to the last row in the result set.

Filter

No

String

Specifies a subset of records to be displayed as a SQL WHERE-clause without the WHERE keyword.

LinkChildFields

Yes

String

Specifies how records in a child subform are linked to records in its parent form.

LinkParentFields

Yes

String

Specifies how records in a child subform are linked to records in its parent form.

Name

Yes

String

The name of the current form.

OrderBy

No

String

Specifies in which order the records should be displayed as a SQL ORDER BY clause without the ORDER BY keywords.

Parent

Yes

Object

The parent of the current form. It can be either a SFDocuments.Form or a SFDocuments.Document object.

RecordSource

No

String

Specifies the source of the data, as a table name, a query name or a SQL statement.

XForm

Yes

UNO
object

The UNO object representing interactions with the form. Refer to XForm and DataForm in the API documentation for detailed information.


Event properties

The properties below return or set URI strings that define the script triggered by the event.

Name

Skriveverna

Basic IDE-beskriving

OnApproveCursorMove

No

Before record change

OnApproveParameter

No

Fill parameters

OnApproveReset

No

Prior to reset

OnApproveRowChange

No

Before record action

OnApproveSubmit

No

Before submitting

OnConfirmDelete

No

Confirm deletion

OnCursorMoved

No

After record change

OnErrorOccurred

No

Error occurred

OnLoaded

No

When loading

OnReloaded

No

When reloading

OnReloading

No

Before reloading

OnResetted

No

After resetting

OnRowChanged

No

After record action

OnUnloaded

No

When unloading

OnUnloading

No

Before unloading


tip

To learn more about URI strings, refer to the Scripting Framework URI Specification.


List of methods in the Form service

Activate
CloseFormDocument
Controls
GetDatabase

MoveFirst
MoveLast
MoveNext
MoveNew

MovePrevious
Requery
SubForms


Activate

Sets the focus on the current Form instance. Returns True if focusing was successful.

The behavior of the Activate method depends on the type of document where the form is located:

Syntaks:

svc.Activate(): bool

Eksempel:

Det følgjande eksempelet går ut frå at du ønskjer å aktiverer skjemaet FormA (skjemaA) på Ark1 i den gjeldande opne Calc-fila. Det får først tilgang til dokumentet med tenesta Document og ThisComponent og aktiverer deretter skjemaet.


     'Gets hold of the form that will be activated
     Dim oDoc as Object, myForm as Object
     Set oDoc = CreateScriptService("Document", ThisComponent)
     

Set myForm = oDoc.Forms("Sheet1", "FormA")

'Activates the form myForm.Activate()
I Python

     doc = CreateScriptService('Document', XSCRIPTCONTEXT.getDocument())
     form = doc.Forms('Sheet1', 'FormA')
     form.Activate()
   
note

ThisComponent gjeld for Calc- og Writer-dokument. For Base-dokument, bruk ThisDataBaseDocument.


CloseFormDocument

Closes the form document containing the actual Form instance. The Form instance is disposed.

Syntaks:

svc.CloseFormDocument(): bool

Eksempel:


      myForm.CloseFormDocument() ' Basic
   
I Python

      form.CloseFormDocument()  # Python
   
note

This method only closes form documents located in Base documents. If the form is stored in a Writer or Calc document, calling CloseFormDocument will have no effect.


Controls

The value returned by the Controls method depends on the arguments provided:

Syntaks:

svc.Controls(opt controlname: str): any

Parametrar:

controlname : A valid control name as a case-sensitive string. If absent, the list of control names is returned as a zero-based array.

Eksempel:


      Dim myForm As Object, myList As Variant, myControl As Object
      Set myForm = myDoc.Forms("myForm")
      myList = myform.Controls()
      Set myControl = myform.Controls("myTextBox") ' SFDocuments.FormControl
   
I Python

      form = doc.Forms('myForm')
      form_names = form.Controls()
      form_control = form.Controls('myTextBox')  # SFDocuments.FormControl
   

GetDatabase

Return a SFDatabases.Database instance giving access to the execution of SQL commands on the database the current form is connected to and/or that is stored in the current Base document.

Each form has its own database connection, except in Base documents where they all share the same connection.

Syntaks:

svc.GetDatabase(opt user: str, opt password: str): svc

Parametrar:

user, password: The login optional parameters (Default = "").

Eksempel:


      Dim myDb As Object ' SFDatabases.Database
      Set myDb = oForm.GetDatabase()
   
I Python

      db = form.GetDatabase()  # SFDatabases.Database
   

MoveFirst

The form cursor is positioned on the first record. Returns True if successful.

Syntaks:

svc.MoveFirst(): bool

Eksempel:


      myForm.MoveFirst() ' Basic
   
I Python

      form.MoveFirst()  # Python
   

MoveLast

The form cursor is positioned on the last record. Returns True if successful.

Syntaks:

svc.MoveLast(): bool

Eksempel:


      myForm.MoveLast() ' Basic
   
I Python

      form.MoveLast()  # Python
   

MoveNew

The form cursor is positioned on the new record area. Returns True if successful.

Syntaks:

svc.MoveNew(): bool

Eksempel:


      myForm.MoveNew() ' Basic
   
I Python

      form.MoveNew()  # Python
   

MoveNext

The form cursor is positioned on the next record. Returns True if successful.

Syntaks:

svc.MoveNext(opt offset: int): bool

Parametrar:

offset: The number of records to go forward (Default = 1).

Eksempel:


      myForm.MoveNext() ' Basic
   
I Python

      form.MoveNext()  # Python
   

MovePrevious

The form cursor is positioned on the previous record. Returns True if successful.

Syntaks:

svc.MovePrevious(opt offset: int): bool

Parametrar:

offset: The number of records to go backwards (Default = 1).

Eksempel:


      myForm.MovePrevious() ' Basic
   

      form.MovePrevious()  # Python
   

Requery

Reloads the current data from the database and refreshes the form. The cursor is positioned on the first record. Returns True if successful.

Syntaks:

svc.Requery(): bool

Eksempel:


      myForm.Requery() ' Basic
   
I Python

      form.Requery()  # Python
   

Subforms

The value returned by the Subforms method depends on the arguments provided:

Syntaks:

svc.Subforms(): str[0..*]

svc.Subforms(subform: str): svc

svc.Subforms(subform: int): svc

Parametrar:

subform: A subform stored in the current Form class instance given by its name or index.

When this argument is absent, the method returns a list of available subforms as a zero-based array. If the form has a single subform, you can set subform = 0 to get access to it.

Eksempel:


      Dim myForm As Object, myList As Variant, mySubform As Object
      myList = myform.Subforms()
      Set mySubform = myForm.Subforms("mySubform") ' SFDocuments.Form
   
I Python

      subform_names = form.Subforms()
     subform = form.Subforms('mySubform')  # SFDocuments.Form
   
warning

Alle Basic-rutinane og -identifikatorane i ScriptForge som vert innleidde med understrek «_» er reserverte for internt bruk. Dei er ikkje tenkt brukte i Basic-makroar.


Støtt oss!