Object variable not set

REM ***** BASIC *****

Sub writeDate
    Dim today As New com.sun.star.util.Date
    today.Month = Month( Now )
    today.Day = Day( Now )
    today.Year = Year( Now )
    form = ThisComponent.DrawPage.Forms(0)  ' first form
    form.datFactuurDatum.BoundField.UpdateDate( today )
End Sub

This macro works fine in the form where I connected it to a button. However, when loading the form for the first time, I get the error message Object variable not set. I am not very familiar with these kind of macros so I probable made a stupid mistake, but but have not been able to solve it.

(Code made preformatted text for readability by @Lupp )

I don’t code much in StarBasic so I may be totally off the mark here, but my immediate thought is that you may need to declare form before using it.

I tried “Dim form” but that didn´t work either.

Was that dim form as object, or ... variant, or did you use a specific forms class (which I have no knowledge of, sorry )?

Hello,

On Ubuntu 20.04.2 Mate with LO v 7.1.4.2 tested with HSQLDB embedded.

Code worked as you stated from a push button. Attached to Open Document event of the form and also had no problem.

Did receive the error when editing the form. This is to be expected as certain elements are not readily available at edit time. You can eliminate this error placing this code at the start of the sub:

xCurrentController = thisComponent.CurrentController
If xCurrentController.isFormDesignMode Then
	Exit Sub
End If

Have tried many combinations of execution and cannot get it to error except when executing on form opening and going to edit the form. All normal.

Your explanation sounds very logical. But, I don’t get the problem in editing (design) mode. I get it when I open the form to enter data. And, if I cancel the macro error, I can use the button and also in the table the correct actual date appears. Do you happen to know if there is another form mode?

@WimVanEk,

Please note your OS, specific LO version database you are using (ie: HSQLDB embedded, MySQL, etc).

If you are getting the error when opening the form, to what event is the macro attached?

Edit:

Not certain what “another form mode” would be. For macros and events, you have the main (external) form(s) in Base. Editing this form and from menu selecting Tools->Customize you can attach macros (Events tab). On the internal form, using the Navigator, you can edit the internal forms’ properties and add macros to events there also.

If you continue to have a problem, consider posting a Base sample (no personal or confidential information) which contains the problem. You can do this in editing your question. See-> How do I attach a file?

Development Environment Mac OS Big Sur (Version 11.4), Libreoffice 7.1.0.2, HSQL
Connected to Push Button on the form with event Mouse Button Pressed and the macro Standard.Module1.writeDate (application, Basic)

When I open the form for adding or editing data the “my macros & dialogs.standard” screen pops up with the error message: Basic runtime error etc.

When I close the error message and “my macros & dialogs.standard” screen, I am in the form of the first record and I can work without problems. I can scroll through the forms / records and use the macro without problems when editing a form / record or when adding a form / record.

@WimVanEk,

Based on you comments, you have the macro attached to another event. Check the places I noted in the above comment or post a sample.

Just to be sure that I gave you the correct info. I opened the form in edit mode and run mode. In edit mode I also received the error message and after adding your proposed code:
xCurrentController = thisComponent.CurrentController
If xCurrentController.isFormDesignMode Then
Exit Sub
End If
The edit error is gone.
However., in run mode not. Therefore I hoped that instead of isFormDesigMode there would be somehing like isFormInitiate/Run/Start etc Mode?

@WimVanEk,

I do not understand why you have not checked the locations I have mentioned two comments ago and one comment ago asked again they be checked. This, in my opinion, is most likely where the problem is. In lieu of that, posting the sample.

Without doing either, I cannot help further.

Also you questioned:

Therefore I hoped that instead of isFormDesigMode there would be somehing like isFormInitiate/Run/Start etc Mode?

the answer being:

If Not xCurrentController.isFormDesignMode Then

but this is useless in your problem. The probable problem is the code being executed by an incorrect event (other than the push button).

Hi, thanks for your support. I am a rookie as far as macros in LibreOffice is concerned I don’t understand your questions, because I am not familiar with the terminology. I will do some more research myself without bothering you.

I think I am getting closer to the solution. The macro is connected to the button. But, there seems indeed another event which starts the macro. I noticed that the macro is even run in another database :slight_smile: That is odd, because the button is not even used there, so there seems to be some general event which runs the macro, no matter which database I open.

I don’t program for connected Forms, but a similar (the same?) effect I sometimes experience with Sub running for a loading spreadsheet document when accessing sheets or specific objects that aren’t completely initialized in time.

One way I tried successfully was to call an initial routine by a helper Sub assigned to the document event ViewCreated.

A different approach is to do it like in

Sub initializeFormNow()
Const maxTries = 20
Dim form As Object, tries As Long
forms = ThisComponent.DrawPage.Forms
While (forms.Count=0) AND (tries<maxTries)
  Wait 1000
  tries = tries + 1
Wend
form = forms(0)
REM Statements as needed
End Sub

The document loader and the frames should run in different threads than the Basic interpreter.