Bind Macros to Form Events in LibreOffice Base

Binding macro scripts to form events in LibreOffice Base allows you to automate tasks, validate user input, and control data flow before it is written to your database. By attaching a macro to specific triggers like the “Before record action” or “Before updating” events, you can intercept record changes, run validation scripts, and cancel erroneous commits before they reach the backend tables.

1. Open the Form in Edit Mode

  1. Open your LibreOffice Base (.odb) database file.
  2. In the main database window, click on the Forms section in the left panel.
  3. Right-click the form you want to modify and select Edit to open it in Form Design view.

2. Open Form Properties

Form-level events (like record updates) must be bound via the Form Properties window, not the individual control properties: 1. Ensure the Form Design toolbar is visible (if not, enable it via View > Toolbars > Form Design). 2. Click the Form Properties icon on the toolbar, or open the Form Navigator (available on the same toolbar), right-click your main form (typically named MainForm), and choose Properties.

  1. In the Form Properties dialog box, select the Events tab.
  2. Scroll through the available events to find the trigger you need:
    • Before record action: Fires immediately before a record is inserted, updated, or deleted, allowing you to validate data or abort the action.
    • Before updating: Available on specific input fields/controls if you only want to validate an individual field before it passes data to the record.

4. Assign the Macro

  1. Click the ellipsis button () located next to the target event (e.g., Before record action).
  2. In the Assign Action dialog, click the Macro… button on the right.
  3. Browse the Macro Selector tree to locate your macro library, module, and the specific procedure you want to execute.
  4. Select the procedure and click OK.
  5. Click OK again in the Assign Action dialog to confirm the assignment.

5. Intercepting and Canceling the Event (Macro Example)

To prevent invalid data from saving during a “Before record action” event, write your macro to inspect values and, if necessary, cancel the event by returning False or stopping the update listener:

Sub ValidateBeforeRecordAction(oEvent As Object)
    Dim oForm As Object
    Dim oField As Object
    
    oForm = oEvent.Source
    oField = oForm.getByName("txtRequiredField")
    
    ' Check if field is empty
    If Trim(oField.Text) = "" Then
        MsgBox "The required field cannot be empty.", 48, "Validation Error"
        
        ' Abort the save action
        ' Depending on the event implementation, you can call reload or discard changes
        ' Returning False or raising an error halts standard execution flow
    End If
End Sub

6. Save and Test the Form

  1. Close the Form Properties dialog.
  2. Save your form changes (File > Save or Ctrl+S).
  3. Exit Design Mode by clicking the Design Mode On/Off toggle icon on the Form Design toolbar.
  4. Enter test data and attempt to save the record to verify that your macro executes as expected.