Page 1 of 1
Macro to execute a simple SQL query in OObase
Posted: Tue Sep 09, 2008 11:02 am
by dargosch
Hi,
I have tree querys I need to execute on the push of a putton in a form in OObase. So, I need a Macro for that.. Is there simple tutorial on how to do this? Just get the query to execute. Nothing will be returned from the query.
/Fredrik
Re: Macro to execute a simple SQL query in OObase
Posted: Tue Sep 09, 2008 2:54 pm
by probe1
Frederik,
see this example. It retrieves a query "
NameOfQuery" from the OOo registered data source "
NameRegDB" and executes it.
Code: Select all
Sub xQuery
' Kontext holen
oDatenbankKontext = CreateUnoService ( "com.sun.star.sdb.DataBaseContext" )
' Datenquelle auswählen
oDatenquelle = oDatenbankKontext.getByName( "NameRegDB" )
sQuery = "NameOfQuery"
' Abfragen
oAbfragen = oDatenquelle.QueryDefinitions
' gewünschte Abfrage vorhanden?
if ( oAbfragen.hasByName( sQuery) ) Then
' Abfrage holen
oAbfrage = oAbfragen.getByName( sQuery )
' SQL-Ausdruck von Abfrage entnehmen
sQuery = oAbfrage.Command
' Verbindung zur DB herstellen
oVerbindung = oDatenquelle.getConnection( ,)
' Statement erzeugen
oStatement = oVerbindung.createStatement
' Abfrage absetzen
oResultSet = oStatement.executeQuery( oAbfrage.Command )
' durch alle Resultate iterieren
while oResultSet.next()
' erste Spalte ausgeben
emplName = oResultSet.getString( 1 )
msgbox emplName
Wend
' DB-Ressourcen wieder freigeben
oResultSet.close()
oStatement.close()
Else
MsgBox "Keine Abfrage mit dem Name " & sQuery & " vorhanden"
Exit Sub
End If
' Verbindung schliessen
oVerbindung.close()
Does this help?
Re: Macro to execute a simple SQL query in OObase
Posted: Tue Sep 09, 2008 3:24 pm
by dargosch
Hi,
Yes, it does. Thank you! Although.. getting the database by name is not something I can do. This will be used within the context of a oobase form, and so the database would not have to me registered (I have nooo idea what the name will be even if it was).
So, how do I get a connection to the database to which the form belongs?
/Fredrik
Re: Macro to execute a simple SQL query in OObase
Posted: Tue Sep 09, 2008 3:57 pm
by QuazzieEvil
the form has a property called
ActiveConneciton which refers to its connection
Code: Select all
Dim Conn As Object
Conn=Form.ActiveConnection