# ThisDatabaseDocument vs ThisComponent?

To automatically start a form when an *.odb file is run, this section of the Base Handbook on page 77 suggests a method:

However I can't get it to run as written because ThisDatabaseDocument is not found.

I scratched my head, then modified it to use ThisComponent in place of ThisDatabaseDocument and it works.

But I'm hoping to understand this better:

1) Why might ThisDatabaseDocument not be found?

2) And given that this works with ThisComponent, what's the difference really between ThisDatabaseDocument and ThisComponent?

3) Is there any reason that I should not be using ThisComponent for this?

edit retag close merge delete

Sort by » oldest newest most voted

@Mike Kaganski has correctly provided the answer (first line in answer) to the question on the difference between ThisComponent and ThisDatabaseDocument with the provided link. However, it seems, based upon further comments, the context of the answer was not fully understood and other misleading, and totally incorrect, comments were made.

The link specifies for ThisDatabaseDocument it is '...only used from a Basic code which is embedded in a Base document.' Now that may not be clear if you do not read it carefully! The code needs to reside in the xxx.odb macro area and NOT in the MyMacros area.

Starting backwards from a form, ThisComponent refers to the form. ThisComponent.Parent refers to the xxx.odb it resides in and ThisComponent.Parent.Parent refers to StarDesktop. ThisDatabaseDocument refers to the xxx.odb and is equal to ThisComponent.Parent.

At the main screen of an xxx.odb, ThisComponent and ThisDatabaseDocument refer to the same thing - the xxx.odb.

Now where the macro code is executed from is another key in usage. You will have code in the document or in MyMacros. With the code in the document the above is true for form and main screen because it is within a database document. If the code resides in MyMacros, it is in StarDesktop. ThisComponent works because it reflects the current document. However, ThisDatabaseDocument will not work because there is no database at the StarDesktop level.

It must be noted, ThisComponent and ThisDatabaseDocument in no way have anything to do with what type of database is being accessed. The above applies to embedded, split and external DB's. This is true whether MySQL on Mint or MariaDB on Debian. They react the same.

Finally, this can all be proven by simply running MRI using the various scenarios presented.

more

Thank you for correcting me, and for carefully explaining the relationship between basic located in xxx.odb versis basic located in StarDesktop. And I didn't realize that there was a simple parent relationship between these two basics. Also now I find that when I pass ThisDatabaseDocument as an object from basic located in xxx.odb to basic code located in MyMacros that I don't need any fussing around, like a delay to get thing to work. Thanks! Boy was I off in left field there for a bit. :-

( 2017-05-23 17:36:51 +0100 )edit

The difference is described here (see at the bottom of the page).

Trying to run the sample database from this FAQ (available from that related question you mentioned) works for me with 5.3.2.2 (x64) on Windows, and the macro there works as expected - so you have some problem that needs to be debugged. You could attach a sample DB which doesn't work for you. [EDIT - here is the above FAQ translated to English]

How do I open and expand a form when opening a database? (Comment ouvrir et agrandir un formulaire lors de l'ouverture d'une base de données ?)

We want to open a form automatically after opening the database. (On souhaite ouvrir automatiquement un formulaire après l'ouverture de la base.)

Base allows to realize this via a macro: (Base permet de réaliser cela via une macro :)

option explicit

global LibOForm as object

Sub LibOFormOpen
ThisDatabaseDocument.CurrentController.connect("","")
LibOForm = ThisDatabaseDocument.FormDocuments.getByName("Instruments").open
End Sub


Use the Tools ▸ Customize ▸ [Tab] Events command to associate this macro with the Open Document event. (Utiliser la commande Outils ▸ Personnaliser ▸ [onglet] Événements pour associer cette macro à l'événément Ouvrir le document.)

Note: (Nota :)

The .open method returns the form object (in the LibOForm variable for this example). This technique will make it possible to manipulate (hide, display, etc.) the form by program via the variable that "represents" it, during the lifetime of the latter (the variable may be global). (La méthode .open permet de renvoyer l'objet formulaire (dans la variable LibOForm pour cet exemple). Cette technique permettra de manipuler (masquer, afficher, etc.) le formulaire par programme via la variable qui le "représente", durant la durée de vie de cette dernière (la variable peut être globale).)

Instruments is the name of the form opened in the attached sample database. (Instruments est le nom du formulaire ouvert dans la base exemple jointe.)

The following variant (Windows) adds the form enlargement after opening: (La variante ci-dessous (Windows) ajoute l'agrandissement du formulaire après l'ouverture :)

option explicit

global LibOForm as object

Sub LibOFormOpen
ThisDatabaseDocument.CurrentController.connect("","")
LibOForm = ThisDatabaseDocument.FormDocuments.getByName("Instruments").open
LibOForm.currentController.frame.ContainerWindow.IsMaximized = true
End Sub


more

@Mike Kaganski, Sorry you feel this way. I had selected your answer, and given you full credit for this, and when people find this answer useful to them in the future, and further vote it up, the karma goes to YOU. I thought I was giving to you. Sorry you see it otherwise. If you want I'll refrain from edits of your posts in the future. I was under the impression that this Q/A site was mostly for the community good, for the next guy who won't have to spend as much time digging for solutions.

( 2017-05-11 15:15:06 +0100 )edit

@Mike Kaganski, I have very much appreciated your help, and the help of everyone here in trying to understand this stuff. Perhaps there needs to be an option to disallow others to edit posts here? Or a better way to identify who's to blame for content? I don't know. It took me quite awhile to translate the French to English so I could read it, and I thought it should be there for others to benefit from. It didn't make any sense for me to put it in my answer, and I couldn't put it in a comment

( 2017-05-11 15:24:24 +0100 )edit

@Mike Kaganski, And even thought you pointed me to this, it only hinted for me at what the difference was. It then took me a bit of messing around before it became clear that the primary difference between the two had to do with the underlying datasource method. So If you want I'll take the stuff out and put it in my Answer. ????

( 2017-05-11 15:29:01 +0100 )edit

@EasyTrieve: I'm sorry for misunderstanding. When I realized that I was wrong thinking that you removed my answer, (because I looked into the wrong topic!) - I removed my stupid words. I apologize for them.

( 2017-05-11 16:01:34 +0100 )edit

We're good. :-)

( 2017-05-11 16:08:36 +0100 )edit

@Mike Kaganski, Now it's time for me to apologize to you, because the intended clarification I had inserted in your answer, and that I have now removed, was incorrect, as @Ratslinger has pointed out here in his answer.

I gave you both a point for your answers, and yours Mike, was the first correct answer, but I have shifted the best answer check mark to Ratslinger's answer, because I felt it is the best overall answer to my question.

Thank you both, for this enlightenment!

( 2017-05-23 17:43:35 +0100 )edit

ThisDatabaseDocument works for me on LO 4.4.1.2 and 5.3.0.2 in Windows with embedded database.

Are you using embeded?

Have you tried putting a delay at the start of the Macro as was mentioned in the related question you referred to?

more

Some of my databases are embedded and some are not. As a temporary workaround, I did try a delay, and will continue to mess around with it. Not sure just how to characterize this issue, but with more testing I might be able to figure out why it sometimes works and sometimes does not. Thanks.

( 2017-05-10 17:07:30 +0100 )edit

The code with ThisDatabasedocument works for me in LO 4.3.7 for Mac OSX and AOO 4.1.3.

more