|
||||||
How to Create a Self Registering Base DatabaseRegister a Database by Automatically Running an OpenOffice.org MacroWith 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:
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 ModuleIt 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 DatabaseOnce 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 FunctionalityThe 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 DatabaseBefore 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 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 RegistrationAs 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:
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.
The copyright of the article How to Create a Self Registering Base Database in Database Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Self Registering Base Database in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||