Automatic field update LibreOffice Base

Dears, good morning!

I want to know if it is possible, after updating the number of items in a table, in a form, that the update date is changed to the current system date, as well as the responsibility for the update is also changed, according to the image below, which I explain:

The “QUANTIDADE” field will be changed, where the “ATUALIZACAO and RESPONSAVEL” fields need to be updated automatically.

In the case of the person responsible for the change, I think that by asking the user to enter the program, and saving this information in a global variable, I can have this data to be used. What I don’t know is to do it via code, through a Macro module.

Thanks for the help.

Hugs!

Dados GM - Cópia.odb

Hello,

There may be multiple ways to do this but need more info. You have not stated your OS, the specific LO version you are using and most importantly here the database you are using (ie: HSQLDB embedded, Firebird embedded, MySQL, etc.). You should always include this minimal information.

Thank you very much for the tip, I will pay attention to this in the future.

I am using Libre Oficce Base Version: 6.1.1.2

Hugs!

Hello,

You still have not provided your OS or the database used. Only item given was the LO version.

Microsoft Windows 7 Profesional

Hello,

Thank you for the sample. Only asked for the database you are using - it is HSQLDB embedded according to the sample posted.

I am the one who thanks you for your patience. Slowly I learn :wink:

Hello,

Am not fond of this process. Can be problematic for many reasons but will present code used. Also concerning presenting code to those who do not know at least the general approach, maintenance in the future with be a problem.

First here is code used:

option explicit

Global iQuantSave as Integer
Global sSavePerson as String

Sub savePerson(oEvent)
Rem Check if Form is in Edit mode - Exit if true
	If thisComponent.CurrentController.isFormDesignMode Then Exit Sub
Rem Save entred name
    sSavePerson=Inputbox("Entee Name:")
End Sub

Sub saveQuant(oEvent)
    Dim oColumns As Object
    Dim oQuant     As Object
Rem Save quantity every time different record is selected
    oColumns = oEvent.Source.getColumns()
    oQuant = oColumns.getByName("QUANTIDADE")
    iQuantSave = oQuant.Value
End Sub

Sub updateFields(oEvent)
    Dim aDate As New com.sun.star.util.Date
    Dim oColumns    As Object
    Dim oQuant        As Object
    Dim oATUAL       As Object
    Dim oRESPON    As Object
    Dim iValue        As Integer
Rem Check if Form event
    If NOT oEvent.Source.supportsService ("com.sun.star.form.component.Form") Then Exit Sub
Rem Check if Update
    If oEvent.Action = 2 then
        oColumns = oEvent.Source.getColumns()
        oQuant = oColumns.getByName("QUANTIDADE")
        iValue = oQuant.Value
Rem Verify value has actually changed
        If iValue <> iQuantSave Then
            oATUAL = oColumns.getByName("ATUALIZACAO")
            oRESPON = oColumns.getByName("RESPONSAVEL")
            aDate.Day = DAY(NOW())
            aDate.Month =  Month(NOW())
            aDate.Year =  YEAR(NOW())
Rem Set Current date
            oATUAL.Value = aDate
Rem Set Name entered
            oRESPON.Value = sSavePerson
        End if
    End if
End sub

Only three routines. savePerson is attached to the OpenDocument event of the form. It is to retrieve the name.

saveQuant is to be attached to the After record change event of the internal form (MainForm). It retains the original quantity.

updateFields is to be attached to the Before record action event of the internal form (MainForm). It is the main routine which verifies the data is actually different and it is an update and not a new or deleted record.

Edit 2020-08-19:

Posting your sample with added code. Note that the code you had on Base opening (believe to hide main form) is dis-connected. It caused me problems. The added code is in frm_Abrir. Also, you will probably want to do further checking on the user inputting their name. Currently the code does no checking.

Sample — AutoUpdateFields.odb

Dear, good morning!

I tried to follow your instructions, but it resulted in the following error message:

BASIC execution error.
Property or method not found: Action.

I even attached the database to the original question to verify the error I mentioned and also to verify that I didn’t make any mistakes when adding the macros.

I added the error message above.

Thanks for the help.

Hugs.

@JoeFatherBr,

My guess is that you may have attached the sub to an incorrect event. Will attach you sample into my answer with the working code shortly.

Thank you!

Dear, tested and approved!

I had reversed the macros!

Thank you!

Hugs!