DrewJensen Super User


Joined: 06 Jul 2005 Posts: 2616 Location: Cumberland, MD
|
Posted: Thu Nov 10, 2005 12:42 pm Post subject: Register ODB file as datasource |
|
|
Here is a small procedure that will allow you to register an existing ODB file as a datasource. I have found it useful in two scenarios -
First, in distributing odb files
Secondly when one needs to change the path information for an existing datasource registration between multiple files.
| Code: |
Sub RegisterDBFile( sURL as String, sRegName as String)
dim oDBContext as Object
dim oDBSource as Object
dim oDBFrame as Object
dim args()
' by opening the file directly we force the context manager to create a temporary entry
' we also don't need to worry about the proper URL type i.e. jdbc, obdc etc.
' as this is all picked up when it is opened
oDBFrame = StarDeskTop.loadcomponentfromURL(sURL,"_Hidden", 0, args())
oDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
' this temp registration is the url to the physical file
' so use it to get a database source
oDBSource = oDBContext.getbyname( sURL)
' and then create a permanent registration entry
' if a datasource is already registered under the name
' sRegname its URL will simply be changed to
' sURL
oDBContext.registerobject(sRegname, oDBSource )
End Sub
|
As an example of using the code I have a small application that I have been dristibuting. The zip file contains the ODB file and a ODT file. The writer file includes a button that calls this routine, which uses a function from the TOOLS library, since both the writer document and the ODB file are located in the same directory at this point.
| Code: |
Sub RegisterODB
dim fPath as string
GlobalScope.BasicLibraries.LoadLibrary( "Tools" )
fPath = DirectoryNameoutofPath(convertFromURL(thisComponent.URL), "\")
RegisterDBFile( convertToURL( fPath & "\teachingdays.odb" ), "teachingdays" )
End Sub
|
After this macros in the document can connect to the registered datasource 'teachingdays'.
Andrew (Drew) Jensen _________________ Blog - http://baseanswers.spaces.live.com/ |
|