0

I have a LibreOffice Base form that allows me to manually add rows to a table. However, the first field of every row is almost always a duplicate of the previous rows first field - Date. Via a macro, I want to automatically fill in the date field so I don't have to manually repeat the information. Using the PriorToReset event handler, I tried the following:

Sub Main

Dim defaultDate              as string
End Sub

Sub PriorToReset(event)
dim Form
dim DateField

Form=event.source
DateField = Form.getByName("Date")

if DateField.Text = "" then 
   defaultDate = Date
   DateField.Text = defaultDate
else
   defaultDate = DateField.Text
end if
End Sub

This does put the current date into an empty row, but when I fill out the remaining fields and attempt to save the row, it objects by saying that the Date field is empty. I'm looking at todays date in that field, but the system acts like its empty. If I backspace over just the last digit, replace it and hit enter it accepts it.

Obviously just dumping the date into the "Text" property does not set the indicator that data has been entered. I also experimented with: DateField.setPropertyValue("Text", defaultDate) But that errors out completely. How do I simulate data entry via a macro?

1 Answer 1

0

Set the date property of DateField using a variable declared as an UNO Date Struct.

First, declare the struct. Then set the values of the individual members of the struct (year, month, day). Then set the date property to equal the struct and commit.

Adapt this code example to suit:

Sub change_a_date

  Dim adate As New com.sun.star.util.Date
  root_form = ThisComponent.Drawpage.Forms
  main_frm = root_form.getByName("MainForm") 

  adate.year = 1990
  adate.month = 7
  adate.day = 4

  main_frm.getByName("date_bx").date = adate
  main_frm.getByName("date_bx").commit

End Sub

Declaration based on example for IsStruct function. See also API reference for UNO Date Struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.