How to use Macro to Update Date Field in Form Record?

End Goal

I am writing a macro for setting the default date for my form for creating a new transaction. When losing focus, if the data (not the text) is empty, the bound field should be updated to Now().

My Problem

Object Variable Not Set (BASIC Runtime Error) happens when I try and update the datefield using

Dim oForm As Object
Dim oTestField As Object
oForm = oEvent.Source.Model.Parent
oTestField = oForm.getByName(“datsale_date”)
oTestField.BoundField.updateDate(Now())

I can’t even find this error in the LibreOffice BASIC docs.

Furthermore, when I try and read the value in either of these ways, I get Incorrect Property Value (BASIC Runtime Error)

MsgBox oForm.getDate(2)
MsgBox oTestField.date

I’m quite sure this is the correct property to read the date from the form (not the record), as this is what the docs says.

Name Datatype Since LO 4.1.1 Property
Date com.sun.star.util.Date Current value (Do not use for values from the data set).

What Else I’ve Done

This is the name of my date field type control field, which is bound to column sale_date.

oTestField = oForm.getByName(“datsale_date”)

I am new to date fields, and I wanted to figure out which property to use, so I used a few other properties to see which one is the one I want. Text property was the only thing I could get that does not throw an error.

Strangely, this returned the date that i last entered, rather than the datsale_date of the record i was currently displaying on my screen. I am also seeking an explanation for this behaviour

MsgBox oTestField.text

How to do it without macro: https://ask.libreoffice.org/uploads/short-url/NRvm738UEGPak4afiayaskIh6m.odb

1 Like

Date for a form control is a struct. You have to define year, month and day separate for this struct.
Example in Base Guide → Changing content of a control

Dim unoDate As New com.sun.star.util.Date
unoDate.Year = Year(Date)
unoDate.Month = Month(Date)
unoDate.Day = Day(Date)
oDateField.BoundField.updateDate( unoDate )
1 Like

Or use CDateToUnoDate

1 Like