Macro to close current form when opening new form window

I am using LO 7.3.2.2 on a Windows 10 system.
I have a macro to allow me to open a new form from a list box but I am not sure how to once the new form name is selected to close the current form.

Here is my Macro:

Option Explicit
Sub Menu_Click(oEvent)
    Dim sFormToOpen As String
    sFormToOpen = oEvent.Source.SelectedItem
    If sFormToOpen = "none" Then
        Exit Sub
    Else
        ThisDatabaseDocument.FormDocuments.getbyname(sFormToOpen).open
    End If
End Sub

Current form will be

aFormStart() = Split(thisComponent.Title, thisComponent.UntitledPrefix)
ThisDatabaseDocument.FormDocuments.getByName( Trim(aFormStart(1)) ).close

Would this go in after the else statement? I am not sure the placement on this in the macro if it would be before the macro opens the new form or after it opens the new form.

Appreciate your advice as I am still learning macro’s

You could try it directly after you have opened the other form. Complete procedure I wrote down for German Base Handbuch:

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

You have stZiel as SelectedItem of a listbox, not as tag from a button, but it will be the same…

For Line 15 I get

BASIC runtime error.
Variable not define.

You have set Option Explicit. So all variables must be declared. DIM aFormStart() could be the solution. Don’t know what is the content of your line 15.

After using the DIM I get a

BASIC Syntax Error. Expected:,.
This is line 15
DIM aFormStart() = Split(thisComponent.Title, thisComponent.UntitledPrefix)

Is there a way around the Option Explicit for this kind of macro?
I have 8 forms that are listed in the list box from a table of form names.
This list box will be on each of the 8 forms for navigation between the forms.
This is a CRM type database I am creating. I am almost done if I can get this part working correctly.

Since it was in German I ran it thru a translator and I think it came out right but this is what I have now.

Option Explicit
Sub Menu_Click(oEvent)
Dim sFormToOpen As String
sFormToOpen = oEvent.Source.SelectedItem
If sFormToOpen = “none” Then
Exit Sub
Else
ThisDatabaseDocument.FormDocuments.getbyname(sFormToOpen).open
End If
End Sub

SUB To_form_from_form(oEvent AS OBJECT)
DIM stTarget AS String
DIM 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

The variable must be declared before it will be used:

DIM aFormStart() 
aFormStart() = Split(thisComponent.Title, thisComponent.UntitledPrefix)

Ok so the it looks like the macro runs until this line which is just before the Open and close statements at the end of the code. Not sure why it’s freezing here. Is this line looking for a button control instead of a list box?

stTarget = oEvent.Source.Model.Tag

ps… I truly appreciate the help with this.

The information in the tag of a control is written down in General → Additional information. If you start this macro by a list box, as you have written, it is oEvent.Source.SelectedItem of your macro.

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

Great thanks,
I was trying to read up on the correct syntax to use.

Everything works as needed.

Greatly appreciated !!! :slight_smile: