OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Register ODB file as datasource

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DrewJensen
Super User
Super User


Joined: 06 Jul 2005
Posts: 2616
Location: Cumberland, MD

PostPosted: Thu Nov 10, 2005 12:42 pm    Post subject: Register ODB file as datasource Reply with quote

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/
Back to top
View user's profile Send private message Send e-mail Visit poster's website
nadim_sd
General User
General User


Joined: 04 Oct 2006
Posts: 30
Location: Lebanon

PostPosted: Thu Nov 02, 2006 3:40 am    Post subject: Reply with quote

Hello,

I know this is an old post but i have a couple of questions:
    - Can we close the odb file after the registration? I tried oDBFrame.dispose(); but it caused problems...
    - Can we modify the connection properties from the macro (procedure)? like letting it point to a different source? or can we for example create a new connection and register it immediately from the macro...

thank you...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group