[Solved] Passing variables from one form to another

Creating and using forms
Post Reply
seglea
Posts: 26
Joined: Tue Nov 18, 2008 1:04 am
Location: Exeter, UK

[Solved] Passing variables from one form to another

Post by seglea »

Is there a way of passing parameter values from a macro attached to one form to a macro attached to another - or for macros attached to one form to read another (open) form's controls (or variables)?

I can see two ways of doing this, neither of which I much like:
(a) use global variables. However this means calling macros from My Macros. This creates a deployment problem in my environment, so I am trying to keep all macros on forms. Of course if 3.0 delivers us macros attached to the application, that will solve the problem, but that doesn't seem to be happening yet.
(b) create a table called parameters within the application, and put anything I want to pass into it by an SQL update query within the calling form, and get it out by a select query within the called form. This seems clumsy, though I can't see anything else wrong with it.

What I'd like to be able to do is either
(c) read from another form directly, by something like s = OtherForm.MessageLabel.Label
or
(d) pass some parameter values in the array of property values that goes as an argument to loadComponentFromURL . However I can't find out either where I would access those properties from within the called form (I've tried the obvious places using the standard properties e.g. OpenWith, and they seem to just give null values or say the property is unknown), or whether the array can be extended to take over user-defined properties.

If I'm trying to do the impossible, I'll fall back on (b). But if there's another way, I'd be very grateful to hear of it. Also if there is some problem I haven't spotted with (b). Thanks.
Last edited by seglea on Sun Nov 23, 2008 8:11 pm, edited 1 time in total.
User avatar
bobban
Volunteer
Posts: 535
Joined: Sat Nov 01, 2008 3:12 pm
Location: Australia

Re: Passing variables from one form to another

Post by bobban »

Just have your macro read from the other form will be easiest imo. Shouldn't be too difficult.

All you have to do is get the document, then the form, then the control and read the appropriate value from it. Getting the control will be trickiest part but not hard, once you have that, the relevant properties (and methods to read and write to them) are obvious.
OOo 3.1.1 on Ms Windows XP
User avatar
voobase
Volunteer
Posts: 97
Joined: Tue Jan 15, 2008 3:07 pm
Location: Australia

Re: Passing variables from one form to another

Post by voobase »

or for macros attached to one form to read another (open) form's controls (or variables)?
Hi there,

Have a dig around these posts to see if they help you. There should not be any problems reading values from what ever forms you have open and with any macro.

http://user.services.openoffice.org/en/ ... =13&t=3619
http://www.oooforum.org/forum/viewtopic.phtml?t=72576

Your b) suggestion can be handy in various situations. I often use tables that have only one row of data for the purpose of holding configuration settings or data which is common to several forms.

Cheers

Voo
OOo 2.3.X on MS Windows Vista
seglea
Posts: 26
Joined: Tue Nov 18, 2008 1:04 am
Location: Exeter, UK

Re: Passing variables from one form to another

Post by seglea »

Many thanks for the advice. With your aid I produced the following function. The name of the form from which I want to extract data is passed in sCallFormName, and the data I want to extract is in the caption of a Label component which is placed on the MainForm of that form; the name of that Label is passed in sRequiredComponentName.

Code: Select all

function LabelFromCallingForm(sCallFormName,sRequiredComponentName) as string
Dim Context,DB,oFormDoc,Connection as Object
Dim oCallingForm,oCallingFormMainForm,oRequiredComponent as object
Context=CreateUnoService("com.sun.star.sdb.DatabaseContext")
DB=Context.getByName("Family address book V4") ' my database name
Connection=DB.getConnection("","")
oFormDoc=DB.DatabaseDocument.FormDocuments()
oCallingForm = oFormDoc.getByName(sCallFormName)
oCallingFormMainForm = oCallingForm.Component.Drawpage.Forms.getbyname("MainForm")
oRequiredComponent = oCallingFormMainForm.getbyname(sRequiredComponentName)
LabelFromCallingForm=oRequiredComponent.Label
end function
This works ok (so long as oCallingForm is open, of course), and I am most grateful for the help.
However I am left with some questions:
(a) Have I gone a long way round here?
(b) What is the function of the Component.Drawpage.Forms bit of the hierarchy? I was expecting to be able to find the MainForm as oCallingForm.getbyname("MainForm")
(c) Why do I have to keep using getbyname? Why wouldn't oCallingFormMainForm.RequiredComponent work directly?
Obviously (b) and (c) take things out into wider considerations, but it would be good to get clarity on (a) before I mark this one as solved.
OOo 2.4.X on Ms Windows XP
User avatar
voobase
Volunteer
Posts: 97
Joined: Tue Jan 15, 2008 3:07 pm
Location: Australia

Re: Passing variables from one form to another

Post by voobase »

Hi again,

a) Yes I think you have gone a long way about it. You shouldn't need to create a database context just to get reference to another form. Try something along these lines instead..

Code: Select all

function LabelFromCallingForm(sCallFormName,sRequiredComponentName) as string
Dim Context,DB,oFormDoc,Connection as Object
Dim oCallingForm,oCallingFormMainForm,oRequiredComponent as object
dim oForm as object

Rem.. assuming sCallFormName is a variable holding the name of the form.
oForm = thisComponent.Parent.FormDocuments 
   if oForm.HasByName(sCallFormName) then     
      oForm = oForm.getByName(sCallFormName)
      if not IsNull(oForm.Component) then    
        oForm = oForm.Component.DrawPage.Forms.getByName("MainForm")

oCallingFormMainForm = oForm
oRequiredComponent = oCallingFormMainForm.getbyname(sRequiredComponentName)
LabelFromCallingForm=oRequiredComponent.Label
end function
b) and c) You might have to search the forum or manuals for answers to that. I'm probably not the best person to explain that stuff. Here is a link to some of the doco I think is good... http://www.oooforum.org/forum/viewtopic.phtml?t=72301

Cheers

Voo
OOo 2.3.X on MS Windows Vista
seglea
Posts: 26
Joined: Tue Nov 18, 2008 1:04 am
Location: Exeter, UK

Re: Passing variables from one form to another

Post by seglea »

Thanks very much Voo. I'm understanding this better now. I am going to mark this one off as solved.
To leave something tidy for others who may have this problem, I have
(a) stripped your code down further (removing redundant dims, mainly)
(b) added in all the endifs and provided a soft landing on failure
(c) eliminated re-use of variables to make it easier to see what is going on.
This leaves us with the following, which I have checked in use.

Code: Select all

function LabelFromCallingForm(sCallFormName,sRequiredComponentName) as string
'returns label of component sRequiredComponentName 
'   from form document sCallFormName
'returns empty string if form or component not found
Dim oFormDocs,oCallFormDoc,oForm,oComponent as object
Dim s as string
s=""
oFormDocs = thisComponent.Parent.FormDocuments
if oFormDocs.HasByName(sCallFormName) then     
  oCallFormDoc = oFormDocs.getByName(sCallFormName)
  if not IsNull(oCallFormDoc.Component) then   
    oForm = oCallFormDoc.Component.DrawPage.Forms.getByName("MainForm")
    if oForm.hasbyname(sRequiredComponentName) then
       oComponent=oForm.getByName(sRequiredComponentName)
       s=oComponent.Label
    end if
  end if
end if
LabelFromCallingForm=s
End function
OOo 2.4.X on Ms Windows XP
User avatar
voobase
Volunteer
Posts: 97
Joined: Tue Jan 15, 2008 3:07 pm
Location: Australia

Re: [Solved] Passing variables from one form to another

Post by voobase »

Hi again,
Thanks for tidying up the code. I did think as I was going to sleep... "oh no, the end if's!" :oops:

Cheers

Voo
OOo 2.3.X on MS Windows Vista
Post Reply