Search code examples
databaseradixlibreoffice-basic

How to get the field values from the forms in base libreOffice?


part of the code

dim oMainForm   as object
dim oColumnList as object 
dim theValue    as variant


oMainForm = ThisDatabaseDocument.FormDocuments.getByName("update_rform")
oColumnList = oMainForm.getByName("rid")  #rid is the name of the field from which I need to get the value
theValue=oColumnList.getCurrentValue()
rid=theValue

When I run the macro, run time error pops up

property or method not found: getByName

I have been searching for a solution the entire day. I came across suggestions like connecting to xray tool or loading the access2base library, but I could not do it. But I do not know, why this is such a difficult task.

I am new to LibreOffice Basic programming, and databases in general.


Solution

  • The following code gets the form document definition only, not an open form, as explained at https://ask.libreoffice.org/en/question/63260/how-to-access-to-the-controls-of-a-base-form-with-basic/?answer=63280#post-id-63280.

    ThisDatabaseDocument.FormDocuments.getByName()
    

    The correct solution depends on how the macro gets called. For example, here is some code that can be called from the main Base screen before any form is opened. Part of the code is from https://ask.libreoffice.org/en/question/7555/open-form-via-macro-in-libreoffice-base/.

    Sub getFormVal
        form_container = ThisDatabaseDocument.FormDocuments.getByName("update_rform")
        form_container.open()
        Wait 500
        oMainForm = form_container.Component.getDrawPage().getForms().getByIndex(0)
        oControl = oMainForm.getByName("rid")
        theValue = oControl.getCurrentValue()
        MsgBox theValue
    End Sub
    

    For ThisDatabaseDocument to work, the code must be in the document, not under My Macros, as explained at https://ask.libreoffice.org/en/question/94670/thisdatabasedocument-vs-thiscomponent/.

    A more elegant approach than getting values from controls is to read columns from the form recordset, as described at https://stackoverflow.com/a/39770933/5100564

    But I do not know, why this is such a difficult task.

    Databases can be difficult to work with anyway, and learning to write LibreOffice Base macros is notoriously difficult. However with enough effort, Base makes many things possible.

    I came across suggestions like connecting to xray tool.

    Yes, an introspection tool like XrayTool or MRI is essential when developing LibreOffice macros.