[Solved] Open a form from a macro

Discuss the database features
Post Reply
dsky
Posts: 7
Joined: Tue Jan 29, 2008 3:59 pm

[Solved] Open a form from a macro

Post by dsky »

I've tried to do this using code examples from many forums, but I always get an error message. Here's my latest effort:

Code: Select all

Sub OpenForm(Event As Object) 
   Dim Form As Object 
   Dim FormDoc AS Object 
   Dim FormCollection As Object 
   Dim DBDoc As Object 
   Dim Args(1) As New com.sun.star.beans.PropertyValue 
    
   Form = Event.Source.Model.Parent 
   FormCollection=Form.Parent 
   FormDoc=FormCollection.Parent    
   DBDoc=FormDoc.Parent 

   Args(0).Name="ActiveConnection" 
   Args(0).Value=Form.ActiveConnection 
   Args(1).Name="OpenMode" 
   Args(1).Value="open" 

   DBDoc.FormDocuments.loadComponentFromURL("NewFormDisplay","_blank",0,Args()) 
End Sub 
I assign this macro to the "Mouse button released" event of a button on a dialog. However, when I run the dialog and click the button, I get a Basic runtime error: "Property or method not found.", pointing to the line "Form = Event.Source.Model.Parent ".

Can someone tell me what I'm doing wrong?

Thanks,
David
Last edited by dsky on Sat Sep 25, 2010 6:06 pm, edited 2 times in total.
Echelon
Posts: 11
Joined: Sun Feb 03, 2008 12:37 pm

Re: Open a form from a macro

Post by Echelon »

I am not certain exactly where you have gone wrong, but here is the code that I use myself.

The following code sets up a function that can be used to open any form.

Code: Select all

Sub OpenForm( oEvent as variant, aFormName as string) as variant
  Dim args(1) As New com.sun.star.beans.PropertyValue
  Dim container as variant

  container = oEvent.Source.Model.Parent.ActiveConnection.Parent.DatabaseDocument.FormDocuments

  args(0).Name = "ActiveConnection"
  args(0).Value = oEvent.Source.Model.Parent.ActiveConnection
  args(1).Name = "OpenMode"
  args(1).Value = "open"
  container.loadComponentFromURL(aFormName,"_blank",0,args())

End Sub
Next you need to add a function that will call this first function and pass it the name of the form you want to open.

Code: Select all

Sub onClickOpenForm ( oEvent as variant )
   OpenForm(oEvent, "My Form Name")
End sub
Using this method you can set up additional functions for all of your forms. For example if you have a form called "Index Form", you could have the method:

Code: Select all

Sub onClickOpenIndexForm ( oEvent as variant )
   OpenForm(oEvent, "Index Form")
End sub
The final step is then to assign the new "onClickOpen..." function to the appropriate button on your form, although I tend to use the "When Initiating" event for the button rather than the "Mouse button released" event
Post Reply