Search code examples
libreofficelibreoffice-basiclibreoffice-base

LibreOffice Base form: filling table or grid control not bound to data source


I have a LibreOffice Base DB with a form and a table control on it. There is a macro that gets some data to put in the table. The table is not bound to a data source and it has 1 column "id". I try the following code to add a row to empty table:

oFormTasks = oCurrentDocument.Forms.getByName("form_tasks")
oGridTasksNotDone = oFormTasks.getByName("grid_tasks_not_done")
oRowSetTasksNotDone = oGridTasksNotDone.getRowSet()
oRowSetTasksNotDone.insertRow()

and get "Function sequence error". What is the correct way to add rows to the table? If it is not possible, can I use some kind of grid control? I need it in a form, not in a dialog. enter image description here


Solution

  • From https://wiki.documentfoundation.org/images/b/b0/BH5009-Macros.pdf:

    For a new record there is a special method, comparable with changing to a new row in a table control. This is done as follows:

    1. Prepare for a new record: oForm.moveToInsertRow()
    2. Enter all wanted/required values. This is done using the updateXxx methods as shown in the previous section.
    3. Confirm the new data with the following command: oForm.insertRow()
    4. The new entry cannot be easily reversed. Instead you will have to delete the new record.

    EDIT:

    Let's start with some background information and then I'll offer a solution.

    The method getRowSet() is misleading, because the table control itself does not have a rowset. Instead, the method gets the rowset of the form that contains the table control.

    Unlike list or combo boxes, it seems that table controls that are unbound cannot have data. It's possible to do columnNames = oGridTasksNotDone.ElementNames but that's about all.

    You can create an UNO grid control in a dialog with code such as oGridModel.GridDataModel = oDataModel, but that will not work in a form.

    A Base form document can contain multiple independent forms by going to Form -> Form Navigator. Note that I am not talking about subforms or a different form document, but rather for example "Form1", "Form2" in the navigator. This allows data to be used in the same document that is not necessarily related to each other.

    To me, the obvious solution is to create a table "Table2" that will be used only for the table control. As explained in the previous paragraph, create a separate toplevel form "Form2" to handle the table control, and set its recordset source to be Table2. Then a macro can populate the table control:

    1. delete all records from the recordset of Form2 (which is Table2)
    2. gather data from Form1 (based on Table1 or Query1 or whatever other data is involved)
    3. insert rows using the recordset of Form2

    Alternatively, if it is required not to use bound controls, you could create Text Tables on the form (Table -> Insert Table) and write macros to insert values into them.

    References: