1

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

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
iyugov
  • 13
  • 4
  • Could you edit the question to describe how you created this example, especially how to define the "id" column? When I tried it I have an empty table control without any columns. It looks like it may be necessary to create column and data models so that new rows can be added properly, something like https://ask.libreoffice.org/en/question/67644/looking-to-create-grid-not-table-control-on-base-form/. – Jim K May 15 '21 at 16:03
  • Okay, I think I understand more after your edit and comment. To add the "id" column I clicked on the control and chose "Insert Column." I'll look into it further and then post a response. – Jim K May 15 '21 at 20:26

1 Answers1

0

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:

Jim K
  • 12,824
  • 2
  • 22
  • 51
  • As I understand, that part is about a dataset bound to the form. I don't have it. And I get the same error message here: "oForm.moveToInsertRow()" – iyugov May 15 '21 at 18:29
  • 1
    Thanks. With a special DB table as a dataset for the table control it works perfectly. With queries in macros I can easliy detele, add, change entries in the DB table and use .reload() method to display the changes. It works even better than I expected as it keeps data in the base for futher processing. Thanks a lot! – iyugov May 16 '21 at 12:46