How to Create a Self Registering Base Database

Register a Database by Automatically Running an OpenOffice.org Macro

How to Create a Self Registering Base Database - Mark Alexander Bain
How to Create a Self Registering Base Database - Mark Alexander Bain
With the release of OpenOffice.org 3.1 the programmer can do something important. They can add a macro to a database and they can run it automatically.

One of the first jobs that the creator of a new OpenOffice.org Base database does is to register the database. They do this so that:

  • all Openoffice.org applications (for example Writer and Calc) are aware of the location of the database and can access it easily
  • the database can be be accessed by a user from any OpenOffice.org applications. This is done by clicking on View | Data Sources or pressing F4 (as shown in figure1 at the bottom of this article)

This, of course, raises an issue for any database developer. If the database is to be distributed to a number of computers then it will have to be registered on each machine. However, that's not so much of a problem since the release of OpenOffice.org 3.1.

The database designer can now store macros in the database file. This means that a programmer can create a self registering database.

Creating a New Database Programming Module

It is now possible for the programmer to create Basic modules (the files used to store macros) in database files (with a .odb) extension, just like they can with Calc or Writer. The process of doing this is discussed in How to Create OpenOffice.org Database Macros.

A New Subroutine For Registering the Database

Once the programmer has opened a new, or existing, module in the database then they can start writing a macro (in this case a subroutine) that will register the database. This macro should be named sensibly and should accept the name of the database to be registered:

Sub selfRegister (dbName as String)

This macro is a subroutine rather a function because no result will be returned.

Access OpenOffice.org Database Functionality

The programmer does not need to understand anything about the process of registering a new database with OpenOffice.org. That's because all of the necessary functionality is included in OpenOffice.org's UNOs (Universal Network Objects). It is, therefore, just a matter of calling the correct UNO:

Dim dbContext As Object : dbContext =

CreateUnoService("com.sun.star.sdb.DatabaseContext")

The next stage is to use the methods associated with this object to carry out the registration.

Registering the Database

Before registering the database the programmer should check that it is not already registered:

If Not dbContext.hasByName (dbName) Then

The registration process then needs two inputs:

  • the name of the database (which is fed in as a variable to the subroutine)
  • the dabase object UNO (obtained bu the UNO by using the URL of the database location)

The first step, therefore, is to obtain the URL of the database:

Dim url as String : url = thisComponent.getUrl

Then the programmer uses the database context UNO to access the database object:

Dim regDb As Object : regDb = dbContext.getByName (url)

And all of the information produced is used to register the database.

dbContext.registerObject (dbName, regDb)
End If
End Sub

The final step is to call the subroutine with the database name to be used for the registration:

Sub Main
Dim dbName : dbName = "eac"
selfRegister(dbName)
End Sub

Of course, this is still a manual process (the subroutine must be run by the user). The next stage is to automate the whole process.

Automating the Database Registration

As well as being able to store macros in a database .odb file, the programmer can run those macros. They can also automate the running of the macros by associating them with document events. In this case it's the document open event that is of interest. If the programmer associates the macro with this event then it will run whenever the user opens the database ensuring that the it is registered correctly.

Once the programmer has created the macro then the process of associating the macro to a document event is quite simple:

  1. Open the database and click on:
    • Tools
    • Customize
  2. In the “Events” tab (as shown in figure 2) select “Open Document” and then click on “Macro”
  3. Select the macro from the .odb file and then click on “OK”

Now the macro will run whenever the database is opened and, if the database has not already been registered before, the macro will carry out the registration automatically.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement