Great - so in the last email we got down to getting the a Form from the Forms container with something like:

DIM aForm
DIM FormContainer

FormContainer=DB.DatabaseDocument.FormDocuments()
aForm = FormContainer.getByName( "MyForm" )

or did we?

What is Form actually?

No doubt by now you have heard/read that in Base a form is really a Writer text file. Yes?

The example above gets an element from the FormContainer, that is not another container, so it must be a form - right?

Wrong.

When you do aForm = FormContainer.getByName( "MyForm" )
what you have is an object of type com.sun.star.comp.dba.ODocumentDefinition
see: http://api.openoffice.org/docs/common/ref/com/sun/star/sdb/DocumentDefinition.html

So your quest to get at the controls on "the form" by just retrieving an item directly from the FormDocuments container is doomed, because you don't have a form yet.

Now what?

If you look back at the documentation for a DocumentContainer
http://api.openoffice.org/docs/common/ref/com/sun/star/sdb/DocumentContainer.html

you will see that another interface that the container offers is xComponentLoader
http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XComponentLoader.html

Which exposes one method: loadComponentFromURL

You already know about this one, since you said you where able to open a form in a macro. Still let's look at a change to your example code one more time:

FormContainer=DB.DatabaseDocument.FormDocuments()
aForm = FormContainer.loadComponentFromURL( "MyForm", ..... )

Now what is aForm?

If is an object, that is a component and supports all of the following services:
com.sun.star.document.OfficeDocument
com.sun.star.text.GenericTextDocument
com.sun.star.text.TextDocument

If you where using Java instead of Basic you would be interested in what each of those does but in Basic you can be a little less concerned.

It is enough to concern yourself with the methods exposed by the component aForm.

The property that we are concerned with first is named DrawPage.
Why? Doesn't really matter, but it is enough for now to know that controls in a Text Document belong to shapes, regions of the draw page. Bottom line is that most of the time this is simply transparent to you as a macro developer..until you start wanting to add controls of your own under program control - then you will need to come back to this.

The DrawPage in tern has an interface of xFormSupplier that is exposed as the method getForms or the property "Forms". In other words it is another type of containers, and just like the FormDocuments container in the DatabaseDocument you can retrieve items from it by index or by name. BUT - it has a twist. It can only hold items of a specific kind. com.sun.star.form.component.DataForm

Documentation for this can be found again at :
http://api.openoffice.org/docs/common/ref/com/sun/star/form/component/DataForm.html

There is also sections in the Basic Programmers Guide referenced before and in the overall Developers Guide.

I mentioned DataForms the other day when we talked about the SingleQueryComposer you might recall.

Before we move on then - to recap. If you want to get at the data controls on a form that is embedded in a Base file you can't just yank out a reference to the form from the FormDocuments container. Instead you must have the FormDocuments container load a component that is the form.

The forum has a property DrawPage that has a property Forms. DrawPage.Forms is a container that holds only components of type DataForm.

It is these DataForm components that have yet one more container, exposed as the property "ControlModels" which is....you guessed it, a container.

Well - at this point I hope that you have gone ahead and grabbed the Xray utility and installed it. If so then you can open your Basic IDE add a new module to any library you like and copy the following this into it and run it... I think you will be able to figure out how to change the line that calls xray.xray so that you can inspect the ControlModel at index 0 in the ControlModels container...*smile*...

REM  *****  BASIC  *****


sub Main
BasicLibraries.LoadLibrary("Xray")
GetControlsInForm
End Sub

Sub GetControlsInForm
Dim Context As Object
Dim DB As Object
Dim FormDoc as Object
Dim FormName as String
Dim Connection As Object
Dim Args(1) As New com.sun.star.beans.PropertyValue
Dim NumControls as Integer
Dim oDoc As Object
Dim oComponents As Object
Dim MyText as String
dim aDoc

Context=CreateUnoService("com.sun.star.sdb.DatabaseContext")
DB=Context.getByName("HERBS")
Connection=DB.getConnection("","")

FormDoc=DB.DatabaseDocument.FormDocuments()
MsgBox FormDoc.Name ' this is the names of the forms
MsgBox FormDoc.Count ' this is the number of forms
NumControls = FormDoc.Count()
For i = 0 To NumControls -1
 sMyMess = FormDoc(i)'.Name
 if sMyMess.Name = "TestCont" then
     aDoc = OpenDBDocument(FormDoc, Connection, "TestCont")
     xray.xray aDoc.DrawPage.Forms(0)
 end if
Next
End Sub

Sub GetControlsInTestCont
msgbox sMyMess
End Sub


function OpenDBDocument(aContainer as new "com.sun.star.comp.dba.ODocumentContainer",_
                       aConn as variant,_
                       aDocName as string,_
                       optional aMode as string _
                       ) as object
Dim aProp(1) As New com.sun.star.beans.PropertyValue
 Dim openMode as string

 on local error goto OpenDBError

 if ismissing( aMode ) then

   openMode = "open"

 else

   openMode = aMode

 end if
aProp(0) = MakePropertyvalue("ActiveConnection", aConn)
 aProp(1) = MakePropertyvalue("OpenMode", openMode)
OpenDBDocument = aContainer.loadComponentFromURL(aDocName, "_blank", 0, aProp())
 wait 1000
 exit function

 OpenDBError:
MsgBox "Error " & Err & ": " & Error$ + chr(13) + "At line : " + Erl + chr(13) + Now , 16 ,"Open Report Error" resume next end function

function MakePropertyvalue(    aName as string,_
                           avalue as variant ) as variant
dim Arg as new com.sun.star.beans.PropertyValue Arg.name = aName
 Arg.value = avalue
 MakePropertyvalue = Arg

end function


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to