Send a parameter to a macro from a button

Hello I have a macro to close a form.

Sub AbrirFormularioMenu(Nameform As String)
ThisDatabaseDocument.FormDocuments.getByName(Nameform).Close
End Sub

It’s called from a button. I have several forms that would call this template.
How can I send the form name as a parameter?

Thanks in advance.

Button Properties:
Action: Open document or URL
URL: .uno:CloseDoc

It’s woks ok, but after closing form, I need to open other form

Sub AbrirFormularioMenu(Nameform As String)
ThisDatabaseDocument.FormDocuments.getByName(Nameform).Close
ThisDatabaseDocument.FormDocuments.getByName(“Menu_All”).Open
End Sub

This dispatches .uno.CloseDoc in StarBasic:

frame  = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(frame, ".uno:CloseDoc,"",0,args())

Just found a StarBasic module in my repository:

sub closeFormDoc(ev)
'mri = createUnoService("mytools.Mri")
doc = getParentObject(ev.Source.Model, "com.sun.star.document.OfficeDocument")
'mri.inspect( doc)
if not (doc.Parent Is Nothing) Then
	closeEmbeddedFormDocument doc.getParent(), doc.getTitle()
else
	 doc.close(False)
endif
End Sub

Function getParentObject(byval m, srvName$)
do until m.supportsService(srvName)
	m = m.getParent()
loop
getParentObject = m
End Function

Sub closeEmbeddedFormDocument(odb, sTitle As String)
Dim ct as String
	forms = odb.FormDocuments
	for i = 0 to forms.getCount() -1
		form = forms.getByIndex(i)
		comp = form.getComponent()
		if not isnull(comp) then
			if  comp.Title = sTitle then 
				form.close()
				exit for
			endif
		endif
	next i
End Sub
1 Like

Alternative macro:

' Close oComp: opened embedded form or report.   
Sub CloseEmbeddedDocument(ByVal oComp As Object)
  Dim oDBDoc As Object, v
  oDBDoc = oComp.Parent
  v = oDBDoc.CurrentController.identifySubComponent(oComp)
  If v.First = com.sun.star.sdb.application.DatabaseObject.FORM Then
    oDBDoc.FormDocuments.getByName(v.Second).Close
  ElseIf v.First = com.sun.star.sdb.application.DatabaseObject.REPORT Then
    oDBDoc.ReportDocuments.getByName(v.Second).Close
  End If  
End Sub

On top of this: designed form opens ok, but I can’t figure out how to close “this” form
(?)
OpenCloseForm

See @Villeroy 's post above.

Couldn’t understand it :grimacing:

The first one is a simple dispatch.
The second one, walks the form hierarchy from any calling object up until the embedding Writer document and calls .c.s.s.util.XCloseable.close.

Different solutions are shown here. But to get the form you want to open you have to send the name of this form together with the button. So the simplest way is to write this in the properties of the button:
Properties: Push Button → General → Additional information
There shouldn’t be any entry. Write the name of the form you will open.
Then add the macro to your code:

SUB FromFormToForm(oEvent AS OBJECT)
 DIM stTarget AS String
 aFormStart() = Split(thisComponent.Title, thisComponent.UntitledPrefix)
 stTarget = oEvent.Source.Model.Tag
 ThisDatabaseDocument.FormDocuments.getByName( Trim(stTarget) ).open
 ThisDatabaseDocument.FormDocuments.getByName( Trim(aFormStart(1)) ).close
END SUB

Current form will be read from the title of the window. Its a little bit more complicated if you have saved forms in separate folders.
Form, which should be opened, will be read from the properties of the button “Additional information”.

Screenshot from 2025-08-26 11-34-29
Screenshot from 2025-08-26 11-34-48

Here is an example database from @Villeroy. :slight_smile:
Open the “Matches” form and click the Close Me button.

Bundesliga_forum 3.odb (136.3 KB)

@CRDF : You have defined “Option Explicit” but haven’t set
DIM args()

The OpeCloseForm routine by @CRDF might fail because ThisComponent.CurrentController is called after opening the new form and possibly becauseThisComponent does not refer to the current form document. frame = StarDesktop.ActiveFrame.Controller.Model may apply in this case before loading the next form.

:flushed:
By the way: what’s the correct Dim for this array?
I did
Dim As Variant
?

Tried it after and before.

Yes, I was aware of the likelihood.
One of the reasons I asked.

But problem was forgetting to declare the array!
RotinaOk

From this answer: I think this is the simplest solution.
getByName must be second part of Title ≠ Name!
As it appears in Forms Section.
E.g. in this case:
Name (in Properties/Navigator) = “FormA”.
So thisForm.Name returns “FormA”, and fails as per hint.
Solution

Dim aFormStart(1) As String
[…]
OpenForm(f)
REM CLOSE THIS FORM:
aFormStart() = Split(thisComponent.Title, thisComponent.UntitledPrefix)
ThisDatabaseDocument.FormDocuments.getByName(aFormStart(1)).close

thisComponent.Title = “NameofDB.odb:FormularioA”.
But what is this split delimiter thisComponent.UntitledPrefix?
If I split by “:” = FAIL.
For display (print) purpose it splits ok
(0) “NameofDB.odb”
(1) “FormularioA”
but .close fails!
?

@CRDF :
thisComponent.Title is
name of database : name of form document
Split(thisComponent.Title, thisComponent.UntitledPrefix)
devides it in an array with
aFormStart(0) → “name of database”
aFormStart(1) → “name of form document”
because thisComponent.UntitledPrefix is : by default

Closing the form works here as expected, because the button is part of the form document.
Edit: Untitled prefix will be : (with a space before : and a space after :). Could be changed so I will use Untitled prefix as a constant variable instead.

I won’t write a new comment here, because there are too much comments from one person, which isn’t the one who wrote the original post. Might be the problem is solved for @jmmarin already…

But with some hidden character…

titulo = Mid(thisComponent.Title, InStr(thisComponent.Title, ": ") + 1)
ThisDatabaseDocument.FormDocuments.getByName(titulo).close

= FAIL
also “:” + 1
But it prints “FormularioA” as it should be.
Mid_Title

But, from a code of Rastlinger:
titulo = Mid(thisComponent.Title, InStr(thisComponent.Title, “:”) + 2)
:ok:
(?)

Here is a more compact and versatile version of the macro for closing any subcomponent:

' Close any subcomponent (Form, Report, Table, Query)
Sub CloseSubComponent2(ByVal oComp As Object)
  Dim oFrame As Object
  If oComp.supportsService("com.sun.star.document.OfficeDocument") Then
     oFrame = oComp.CurrentController.Frame
     oComp.setModified False    ' Cancel the dialog if changes are not saved
  Else
    oFrame = oComp.Frame
  End If  
  CreateUnoService("com.sun.star.frame.DispatchHelper").executeDispatch(oFrame, ".uno:CloseDoc", "", 0, Array())      
End Sub
1 Like