WrappedTargetException

I’m on Windows 10, running LO 7.0.6.2, and I’m using Base for a frontend on my MySQL database. In my macro (started by the Base form document “Adding Authorizations” opening), I have the line (which I got from a solution posted here by @RobertG .)

ThisDatabaseDocument.FormDocuments.getbyname("Adding Authorizations").close

I get the error message from that line

BASIC runtime error.
An exception occurred
Type: com.sun.star.lang.WrappedTargetException
Message:
TargetException:
Type: com.sun.star.embed.StateChangeInProgressException
Message: .

I’m not sure if the WrappedTargetException is because I’m trying to close the form that’s just opening. Anyone know what the problem is and what I can do to fix it? Basically, The objective is to run a query about the current database user and based on the result, close the database form.

To give a little better context, This is part of my macro:


	oSQLStatement = oDB.createStatement
	sSQL = "select user()"
	Result = oSQLStatement.executeQuery(sSQL)
	Result.first
	sUser = Result.getString(1)
	'get the database user name by splitting off the @hostname
	mUser = Split(sUser, "@")
	'get the id number of the user
	sSQL = "select id from Personnel where login = '" & mUser(0) & "'"
	Result = oSQLStatement.executeQuery(sSQL)
	Result.first
	iUser = Result.getInt(1)
	sSQL = "select count(id) from authorizations where authtype = 14 and personnel = " & iUser
	Result = oSQLStatement.executeQuery(sSQL)
	Result.First
	If Result.getInt(1) = 0 Then 'Current user has not been authorized to grant authorizations.  So Display message to that effect and close the Adding Authorizations form document
		msgBox "You have not been authorized to grant authorizations! Please see the Laboratory Administrator."
		wait 1000
		ThisDataBaseDocument.FormDocuments.getbyname("Adding Authorizations").close
		Exit Sub
	End If

The reason I’m using the macro started by the form opening is because the unauthorized user could open the form directly from the main Base window.

Don’t know why you want to close a form, which just has been opened, but could be a WAIT 1000 (1 second time out) before closing the form could help.

Closing the form seemed like the most effective way to prevent the user from doing something they’re not authorized to do.

My apologies. Have withdrawn my comment(s) and answer after rereading your question. It appears there is more to this as you are also mentioning about running a query. Don’t even know what database your are using (or LO version or OS).
.
So it is more than just closing a form after opening. Is this correct?
.
The code you used worked for me in an internal form event:
.
Screenshot at 2022-01-25 20-03-52
.
but not from a form event.

@RobertG Thanks, but unfortunately, adding the one-second timeout did not fix the problem. It still threw the error.

@Ratslinger My turn to apologize for shorting you information. I have added the details of the situation to my initial post.

@agerber85,What about placing it, as in my comment, on the internal form event of When loading? The close statement worked there for me.

Yes, that did it! Thanks! I realize now that I need to figure out a more clear way to refer to forms in Base. Do you think it would be clear to refer to internal forms (as you have done) vs. form documents (since the forms opened by base from the main window are essentially Writer docs)?

@agerber85,

Where to place a macro depends a lot upon just what is being done. Quite often I use the View created or Open Document event. In your particular case here, it may be trying to close the form in a process where the opening process is not fully completed.

Sometimes it is trial and error.