Libreoffice Base: Need today’s date as default value in a form field

datelibreofficelibreoffice-base

New to libreoffice Base. I've found how to set the default value in a table to the current date. Now I need to have the current date show up in the corresponding field in a form. I have tried inserting into "default value" TODAY() and CURRENT_DATE. They both give me Nov. 18, 1899 for some reason. And ideas how to do this?

Best Answer

Use the following macro code. Here, the table's column (not the control name) is called "MyDate".

Sub DefaultDateInForm (oEvent As Object)
    oForm = oEvent.Source
    lDateCol = oForm.findColumn("MyDate")
    If oForm.getString(lDateCol) = "" Then
        dateStamp = Format(Now, "YYYY-MM-DD")
        oForm.updateString(lDateCol, dateStamp)
    End If
End Sub

Edit the form, and in form properties, assign the macro to the "after record change" event.

form properties

Now, whenever a record's date is empty, such as when a new record is started, the date field should default to the current date.

There are several discussions about this topic on the openoffice forum: