Page 1 of 1
[Solved] Opening a form with a button
Posted: Wed Jan 27, 2016 9:31 am
by RobynsEgg
I've been reading the forums extensively, trying to figure out how to open a form from within a (different) form. I think I should use a button, and I think the correct way to do it is under the Action heading with .uno:open (though I would use a macro if that would do it too.) However, trying to find the correct syntax for it (or learning the .uno framework at all, actually) has been an exercise in futility?
UPDATE: many useful examples in here. Turned out not to be a coding issue at all but a button (Form) issue...
Re: Opening a form with a button
Posted: Wed Jan 27, 2016 5:49 pm
by Villeroy
No such URLs for embedded documents. Since 11 years this is a missing feature. Get your forms out of the Base jail, use the switchboard extension or your own macro program.
[Tutorial] Standalone Forms / Switchboard
Re: Opening a form with a button
Posted: Wed Jan 27, 2016 7:07 pm
by MTP
The macro syntax in StarBasic to open an embedded form document from a different embedded form document is
Code: Select all
ThisDatabaseDocument.FormDocuments.getbyname("NewDocumentName").open
Re: Opening a form with a button
Posted: Thu Jan 28, 2016 11:15 am
by RobynsEgg
I have tried using
Code: Select all
sub openfrmTrack
ThisDatabaseDocument.FormDocuments.getbyname("frmTrackAdd").open
End Sub
which returned nothing. No errors, it just didn't do anything.
Then I tried using this (which I adapted from another forum topic)
Code: Select all
REM *****Open Contact Data Form*****
Sub OpenContactForm
Dim sNewDocumentName as string
sNewDocumentName="frmTrackadd"
oNewDoc = ThisDatabaseDocument.FormDocuments.getbyname(sNewDocumentName).open
oNewDoc.DrawPage.Forms.GetByIndex(0)
End Sub
Which got the same result. What am I missing?
Re: Opening a form with a button
Posted: Thu Jan 28, 2016 11:22 am
by RoryOF
When posting code, please use PostReply or FullEditor windows and use the Code button. This makes it easier to read the code and makes it more obvious that it is code.
Re: Opening a form with a button
Posted: Thu Jan 28, 2016 3:01 pm
by RPG
Hello
RobynsEgg wrote:What am I missing?
Maybe you expect something more then opening the form but then you have wrong ideas about your code. I think start study
this.
Romke
Re: Opening a form with a button
Posted: Thu Jan 28, 2016 6:26 pm
by UnklDonald418
Based on your profile information you might consider upgrading to a later version of oO.
Here is something I use based on a post found here a while back. It might work for you.
Place the name of the form you want to open in Button->Properties->General->Additional Information of a Button linked to this macro. It will open/display the named form, and if you add a comma before a second form name the second form will be closed.
Code: Select all
REM ***** BASIC ****
'----- suite to open any form
Sub OpenFormCloseForm_Button_Click1( oEv as variant )
'reads sOpenFormName & sCloseFormName separated by a comma from tag of calling button
Dim N1 as integer,N2 as integer, xpos as integer, ypos as integer
Dim XX as string, sOpenFormName as string, sCloseFormName as string
' Globalscope.BasicLibraries.LoadLibrary( "MRILib" )
' oMRI = CreateUnoService( "mytools.Mri" )
XX=oEv.Source.Model.Tag ' get button's form from Additional information on button control
N1=len(XX)
N2=instr(XX,",")
If N2<>0 then
sOpenFormName = left(XX,N2-1)
sCloseFormName = right(XX,N1-N2)
else
sOpenFormName = XX
sCloseFormName = ""
endif
' this Basic routine shows different results for a Text, Spreadsheet and Presentation document
' Depending on the document type, a different set of interfaces are supported by the object
' A portion of the interfaces are common to all these document types representing
' the general functionality that documents of any type offer. In particular, all OpenOffice.org
' documents support the com.sun.star.document.OfficeDocument service, including the interfaces
' com.sun.star.frame.XStorable and com.sun.star.view.XPrintable. Another interface is
' com.sun.star.frame.XModel.
' MsgBox ThisComponent.Dbg_SupportedInterfaces
If sOpenFormName<>"" then OpenForm( getFormsTC, getConnectionTC, sOpenFormName )
If sCloseFormName<>"" then ThisDatabaseDocument.FormDocuments.GetByName(sCloseFormName).close
End Sub
Function OpenForm( formContainer as variant, oConnection as variant, sFormName as string) as variant
Dim aProp(1) As New com.sun.star.beans.PropertyValue
aProp(0).Name = "ActiveConnection"
aProp(0).Value = oConnection
aProp(1).Name = "OpenMode"
aProp(1).Value = "open"
OpenForm = formContainer.loadComponentFromURL(sFormName,"_blank",0,aProp())
End Function
Re: Opening a form with a button
Posted: Fri Jan 29, 2016 12:07 am
by RobynsEgg
@RoryOF - Will do in future.

@RPG - But it doesn't actually open the form? Will read the help file given
@UnklDonald - My apologies, I updated to 4.1.2 a while ago but failed to realize it does not automatically update my forum tag.

- I did read that in your earlier forum post but it didn't seem relevant in this case, which is why I did not include it? I JUST want to open the other form.
Re: Opening a form with a button
Posted: Fri Jan 29, 2016 6:36 am
by UnklDonald418
Either I don't understand what you mean by
want to open the other form
or you don't understand the macro presented above. It is designed to be reusable. Once installed there is no need to write a new macro every time you want to open a different form.
You create a button that points to this macro, and edit the button's properties with the name of the form you want to open. You can have one button on or whole menus of buttons calling this one macro.
Re: Opening a form with a button
Posted: Fri Jan 29, 2016 4:50 pm
by MTP
Form2 in the attachment has three buttons. The first button calls the code I originally posted to open Form1, the second and third call the code posted by UnklDonald418 (the second just opens Form1, the third opens Form1 plus closes Form2).
All buttons work as expected on my system. Do they work on yours?
Re: Opening a form with a button
Posted: Mon Feb 01, 2016 10:56 am
by RobynsEgg
@MTP - Incredibly, the answer is NO, those buttons also do nothing when I click on them...and that tells me what I was missing, and I corrected the security settings of my security programs and openoffice. My macro still didn't work, but I was able to learn from your example.
The first macro you used is the one I was asking for, just a simple oneliner to open the form. Sorry if I was confusing people.
Thank you so much!
UPDATE: It's my buttons that don't work! The buttons that were created on the sample demo that I was given work fine...it is just buttons I create that don't fire at all (even if I cut and paste the exact same code, word for word. (I should have gotten an error code "Form1 does not exist"... but nothing.
...though I realize that is another issue entire entirely so I will examine the forums before (if necessary) looking for guidance that way.
Re: (SOLVED) Opening a form with a button
Posted: Mon Feb 01, 2016 4:35 pm
by MTP
When you create a button, look at the properties window - on the tab "Events" are you pressing the "..." button next to "Execute" and then selecting your macro in the popup?
[Solved] Re: Opening a form with a button
Posted: Thu Jun 07, 2018 11:27 am
by chris-nz
RobynsEgg wrote:@MTP - Incredibly, the answer is NO, those buttons also do nothing when I click on them...and that tells me what I was missing, and I corrected the security settings of my security programs and openoffice. My macro still didn't work, but I was able to learn from your example.
The first macro you used is the one I was asking for, just a simple oneliner to open the form. Sorry if I was confusing people.
Thank you so much!
UPDATE: It's my buttons that don't work! The buttons that were created on the sample demo that I was given work fine...it is just buttons I create that don't fire at all (even if I cut and paste the exact same code, word for word. (I should have gotten an error code "Form1 does not exist"... but nothing.
I had the same problem of the macro not executing when pressing the button, and once i set the Tools->Options->Open Office->Security->Macro Security then, i found that i needed to close down Open Office completely then reopen it before the macro would be executed by the button being pressed.

Re: [Solved] Opening a form with a button
Posted: Tue Aug 27, 2024 9:58 am
by Yumi
Thanks for this a little older post, still solved my problem in 2024.
I only wondered why the macro always opened Form1 whatever I changed. Until I found in base - button control-properties - General when I scroll all the way down there is a field "Additional Information". This is where the name of the form to open and that of the form to close is entered.
Example: AA_Startpage,AM_Parameters (the comma is important for base to know which to close)
Hope the extra little info helps another beginner to macros and forms.