Clearing Date Fields using a Macro

I’m having some difficulty clearing the contents of a Date Field in Libreoffice Base’s form using a macro.
I can retrieve all of the data just fine, and clear the comboboxes OK but clearing the dates doesn’t seem to work. Am I missing something?

Here’s what I’ve been trying:

Additional note - this clears the .text property just fine, but it doesn’t clear the visible contents of the date fields of the form.

Date fields can be cleared in a few ways, but the wanted end result is more important. The first way is pretty simple:

Sub ClearField
	oForm = ThisComponent.Drawpage.Forms.getByName("MainForm") 'Get Form'
	oField = oForm.getByName("dateOrder")
	oField.Text = " "
	oField.Text = ""
End Sub

The reason for the two oField.Text lines is that first you must move something into the field before you can change it to empty. You can’t leave it as a space because it is not a valid date. Another way is to do it through the view:

Sub ClearField
	oForm = ThisComponent.Drawpage.Forms.getByName("MainForm") 'Get Form'
	Doc = ThisComponent
	oField = oForm.getByName("dateOrder")
	DocCtl = Doc.getCurrentController()
'Get VIEW for listbox'
  	CtlView = DocCtl.GetControl(oField)
	CtlView.setEmpty()
End Sub

This will clear the viewed field just as the first example. Just know that is all these two routines do - remove the viewed content.

If this is a database field, however, and you are trying to delete it, more needs to be done. Adding this line:

		CtlView.setFocus()

to the end of the last routine will prepare the field to be updated as a blank field (if you’ve allowed that) and adding this line:

		oForm.updateRow()

completes the update of the blank date field. To actually change the date use:

		Dim aDate As New com.sun.star.util.Date
	'Used to get the date'
		aDate = CtlView.getDate()
		myDay = aDate.Day
		myMonth = aDate.Month
		myYear = aDate.Year
	'Used to set a date'
		aDate.Day = 02
		aDate.Month = 28
		aDate.Year = 16
		CtlView.setDate(aDate)

Thank you!

It doesn’t seem to work for me. Am I missing something?Date field.odsDate field multiple.ods

Am I missing something?

Yes, several things:

  1. The topic is about Base - you are asking about Calc;
  2. Answers are to answer original question; your “answer” is either a comment to @Ratslinger’s answer, or an own answer, where you could write something like “In question “link-here”, there’s an answer that doesn’t seem to work for [this document]”.

@giacomoleopardo When working with Calc, you need to get the sheet first then the DrawPage. Also your internal form name is incorrect. This macro works with your file:

Sub ClearField
    oSheets = ThisComponent.getSheets()
    oObj1 = oSheets.getByIndex(0)
    oForm = oObj1.Drawpage.Forms.getByName("Formulario")
    oField = oForm.getByName("dateOrder")
    oField.Text = " "
    oField.Text = ""
End Sub

Thank you. One last question: what if I need to clear more than one date field and a few check boxes?
attached file in my previous answer

@giacomoleopardo Controls are accessed by name on the form they reside on. The original question in this post has been answered. If you need further assistance for your needs, please ask as a new question.